What internal method calls in javascript when I get the value of an array element by index?

I have a code wtfjs:

var a = [,];
alert(a.indexOf(a[0]));

a.indexOf(a[0])returns -1. Central to this example is the difference between the values uninitializedand undefined:

a contains one non-initialized element.

a[0]return statement undefined.

adoes not contain a value undefined. So a.indexOf(a[0]) === -1 true.

But where can I find an explanation why a[0]return undefined? What internal method calls?

PS undefinedis a primitive javascript type. uninitializedmeans a value that has no javascript type, but there is no such primitive type in javascript.

+4
source share
4 answers

ES5 :

.

, . undefined. , :

... .

indexOf , , :

kPresent [[HasProperty]] O ToString (k).

k - , , O - . , .

+6

.indexOf() , . , 1 ( 2, ), , .

:

var a = [,];
console.log(a.length); // 1 (in Firefox)
console.log('0' in a); // false

, 1, 0. , a[0] undefined.

, :

a[0] = undefined;
console.log(a.length); // still 1
console.log('0' in a); // true !!

, "", ( ) undefined.

" ", , , "Get".

+1

. , a = [, 1] , , 2 , 1. 0 1 , undefined. , . undefined , -1. null, , .

, a.indexOf(a [0]) -1, [0] - undefined.

0

, ( ), undefined.

:

var a = [,null,];

a [0] [2] - undefined. , a, undefined a[1], . length , undefined. :

var b = [];
b[10] = null;
b.length
> 11

, undefined, b[10].

: http://msdn.microsoft.com/en-us/library/d8ez24f2(v=vs.94).aspx

, - , undefined JavaScript.

0

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


All Articles