Friday 15 January 2010

javascript - Learning Array.prototype and calculating its length -


First: I recently knew that Array.prototype The self is an array ([]). But one amazing thing is that this is not an example of array objects. How is this possible? What is the reason for this?

Second: array. Protept has many properties but when you log in ie console.log (Array.prototype.length) < / Code>, output is '0' . Is the reason here? I also tried, the result is similar

  var x = []; X ['one'] = "b"; X ['b'] = "c"; Console.log (x.length) // Output: 0   

It would be great if I know the difference in the element and property of an array

I recently knew that Array.prototype is an array ( [] < / Code>).

You are right, this is an array in the specification that says:

Array prototype object is an array; Its [[class]] is "sir" , and it has the property (the initial value is +0 ) And special [[DefineOwnProperty]] is described in the internal method.


But one of the surprising thing is that it is not an example of the array object how is it possible? What is the reason for this?

If you try Array.prototype instanceof Array , the result will actually be false . Why? The way the example operator works, it compares the prototype of an object with the value of the property of the prototype of the constructor function.
I.e. In this case it does

  Object.getPrototypeOf (Array.prototype) === Array.prototype   

As we have already done in this comparison You can see, we are trying to check whether Array.prototype has its self prototype, which is impossible in the specification also mentioned in the same paragraph :

The value of [[prototype]] The internal property of the Aero prototype object is the standard built-in object prototype Ject ().

That is, Object.getPrototypeOf (array.prototype) === object.prototype and Object.prototype! == Array .prototype . Therefore instanceof yields false , but array.prototype is still an array.


Array.prototype has many properties but when you log in console.log (Array.prototype.length) < / Code>, output is '0'.

0 is defined in the Array.prototype.length specification (see above).

It would be nice if you know the difference in the element and property of an array

An element array property properties Is a property with positive 32-bit integers (or 0 ). A property is any other property in which such property is not named, it is not considered by the operation of any array.

The specification provides more accurate descriptions than this:

Array objects provide special treatment for a certain class of property names. A property name P (as a string value) is an array index and if only if toString (ToUint32 (P)) is <<> ToUint32 (P) 2 is not equivalent to Code> P and 32 A property whose name is an array index is Element is also called.

You see, if a property name (representing string) is an unsigned 32-bit integer and still the same value, then this is an array index and the associated value of the array There is an element.

length The value of the property is numerically greater than the name of each property whose name is an array index;

We have just learned which property names are considered to be array indexes, which can be converted into unsigned integers, by that definition, "a" is not an array index, so

  var x = []; X ['A'] = 42;   

length does not change the property but "3" is an array index, so

 < Code> x ["3"] = 42; Converts property to  4    

length

No comments:

Post a Comment