0

I'm bored and messing with my console and I came up with the following code:

I was trying something like:

x = 16;
y = 26;
f = { x+y:"String!"};

expecting something or somehow to do:

Object {1626: "String!"}

Or at least

Object {42: "String!"}

I ended up with

x = 16;
y = 26;
eval("f = { "+x+y+":\"String!\"}");

Which returned as expected:

Object {1626: "String!"}

I've always been told to avoid eval() and never even think about using it due to something about security. Is there a way to use variables in declaring the property (Sorry if I don't know what it's called exactly)

Codingale
  • 220
  • 6
  • 17
  • 3
    `f = {}; f[x+y] = "String"; f[x.toString() + y.toString()] = "String";` – Hunter McMillen May 10 '14 at 21:21
  • Do you happen to know the exact type of variable `{foo: "bar"}` is so I can look up more online, `typeof()` only says object... – Codingale May 10 '14 at 21:24
  • That is only an `object`... That is its exact type. – Paul May 10 '14 at 21:25
  • You're looking for [Bracket Notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Member_Operators#Bracket_notation) – Paul May 10 '14 at 21:27
  • Well `typeof(Array("foo","bar"))` returns object also, but it's an array like `["Foo","Bar"], so what makes them different? – Codingale May 10 '14 at 21:29
  • 2
    @Codingale Yes, but `Array("foo", "bar") instanceof Array` will also return true. All arrays are objects in Javascript, but not all objects are arrays. Functions are also objects. – Paul May 10 '14 at 21:34
  • and http://stackoverflow.com/questions/11508463/javascript-set-object-key-by-variable – Felix Kling May 10 '14 at 21:37
  • Yeah it's definitely a duplicate, however I'm not sure if you're linking them for the future people to find or just to link them.... I simply phrased it differently and had no resulted that made the search find one of those questions. – Codingale May 10 '14 at 21:40
  • Why does `Array("foo","bar") instanceof Object` return true? I know it's an object and an array, however what makes instanceof Object return false? Something like if it's a number or something? – Codingale May 10 '14 at 21:43
  • That's the point of closing a question as duplicate, it will be easier to find via other search terms. The closed questions are pointers to the "original" question. I don't understand your last comment. You say first `instanceof Object` is `true` and then you say it's `false` (which it isn't). What are you confused about? – Felix Kling May 10 '14 at 22:20
  • @Codingale Note that there is no shame in having a question closed as a duplicate. If you didn't know what to search for, that is fine, but it is helpful to find duplicates, both so that you can read relevant answers, and other people that search for using the same keywords as you used in your question can get directed to those answers as well. – Paul May 10 '14 at 22:59
  • Is there ever a case where `instanceof Object` return false? Maybe if it's a number or something I guess? – Codingale May 12 '14 at 01:50
  • @Codingale Yes, a primary type is not an object, for example `7 instanceof Object` is false, but `new Number(7) instanceof Object` is true. – Paul May 12 '14 at 05:47
  • @Codingale Also primary types are promoted to objects if you use them as such: `Object.prototype.foo = function(){ console.log('bar'); }; 7..foo();` – Paul May 12 '14 at 05:49

2 Answers2

2
x = 16;
y = 26;
f = {};
f[x+''+y] = "String!"; // For f[1626]

or

f[x+y] = "String!"     // For f[42]
Drazen Bjelovuk
  • 5,201
  • 5
  • 37
  • 64
1

I made this exact same mistake when I started learning JavaScript: JavaScript set object key by variable

You first must make the object:

var f = {};

Then you can use variables to dynamically create keys:

var x = 16, y = 26;
f[x+y] = "Integer"; 
f[x.toString() + y.toString()] = "String";
Community
  • 1
  • 1
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • I like how you showed both ways for `f[42]` and `f[1626]`, I wasn't sure how to phrase the question just like you, thanks for the answer! – Codingale May 10 '14 at 21:36