Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats going on 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 whats going on, it seems like an object with an object as a property, but then i dont see why the second example is undefined.

like image 537
Leathan Avatar asked Jul 22 '17 18:07

Leathan


1 Answers

So here is what I guess is happening.

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

So, when you are doing,

global[{a}] = 7;

It's similar to global["[object Object]"] = 7

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

Now , for this

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

You haven't declared b as object that's why you are getting answer as undefined.

If you do the following, result would be same;

b = {};
b[{a}]=7;
b[{a}] // returns 7
like image 158
Anurag Singh Bisht Avatar answered Sep 28 '22 18:09

Anurag Singh Bisht