What is happening here global [{a}] = 7

I have the following code.

a=7
global[{a}]=7
global[{a}] // returns 7

b[{a}]=7
b[{a}] // returns undefined

I honestly have no idea what is happening, it looks like an object with an object as a property, but then I donโ€™t understand why the second example is undefined.

+6
source share
2 answers

So, this is what I think is happening.

As you mentioned earlier, the global value is the same as the object window.

So when you do

global[{a}] = 7;

He looks like global["[object Object]"] = 7

Then you will get the answer :: global[{}]how 7.

Now for this

b[{a}]=7
b[{a}] // returns undefined

You have not declared bas an object why you get the answer as undefined.

If you do the following, the result will be the same:

b = {};
b[{a}]=7;
b[{a}] // returns 7
+3
source

-, , , , . [object Object]

global[{a}], global["[object Object]"] /

a = 7
b = {}

b[{a}] = 7
console.log(b[{ a }]) // returns 7
console.log(b["[object Object]"]) // returns 7
Hide result
+2

All Articles