Array and objects in javascript

If the length is passed in square brackets as an array. It returns the first element. What is the main logic here?

var foo= [];                                        
foo['0'] = 'hello';
foo[2] = {'fun': true};

foo[length];
+4
source share
1 answer

If you use this code in a browser, this is because an object window(which is a global object) has a property lengththat represents the number of child view contexts (iframe, etc.) that it has. This 0, if they are not (which often happens). All the properties of the object windoware global, so lengthitself is lengthon windowso foo[length]- foo[0]that is "hello".

length foo. foo foo.length. , undefined, foo.length 3, foo[3].

+6

Source: https://habr.com/ru/post/1666827/


All Articles