From the topic, the reason is not complete, because there are some things such as array of methods how to use this I do not intend to say, because that look will, the following is what I think is important, only about the array object itself. In addition, because my JavaScript actual combat experience is not much, so maybe some things did not involve, some content said wrong, please find the problem of the classmate feel free.
First of all, Javascript (JS) of the array definition, this is not the focus, in short, the following two sentences are to create an empty array:
var arr = [];
var arr2 = new Array (); It's OK not to write new.
Once created, you can add elements to the array at any time. The size of the array is not fixed and can be added arbitrarily, like a[0] = 1.
And then to the point, about adding elements to the array. First, you need to know that the array is an object, and the object is a set of key-value pairs (similar to the dictionary in the dict,c# inside the Java Map,python), the object can have attributes, the object's function is called the method, A property or method of an object can be accessed using a square bracket or a point number. where you use brackets to refer to quotes, the use of the dot can only be used when the property name is a valid variable name, that is, if the attribute does not contain any whitespace hyphens and does not begin with a number, see example:
var person = {};
Person.age =;
Person.sayhi = function () {console.log (' Hi ');
Person.age;
person[' age '];//
Person.sayhi ();//Hi
person[' sayhi '] ()//Hi
Well, this is the object, as if it's nothing special (except with square brackets), but that's enough for the object to say, and then start with the array.
For the above, the array can be done, that is to say, the following code will work (only the first line is different from the above):
var person = [];
Person.age =;
Person.sayhi = function () {console.log (' Hi ');
Person.age;
person[' age '];//
Person.sayhi ();//Hi
person[' sayhi '] ()//Hi
Since an array is an object, do not confuse the string index of the square brackets with the usual numeric index, yet start to say (right away) the digital index.
Unlike ordinary objects, the elements of an array object have a numeric index, or a special key (previously said to be a key-value pair), which is the same as the array we see in other languages such as Java, C #, and so on. In JS, this key has some special requirements, it can be a number, but also can be converted into a number of strings, and reasonable numbers, need to be in the range of 0 to 4294967295 (2^32-1) of integers (in fact, this index in the lexical analysis as a string, JS converts this string into a 32-bit integer and then converts a 32-bit integer to a string compared to the original string, which, if the same, indicates that the index value is a valid number or is an ordinary string key. As a simple example:
A = [1, 3, 5, 7];
Console.log (A[0]); 1
console.log (a[' 0 ']);//1
a[' 2 '] =;
Console.log (a[2]); 12
The above code can be run in the browser, the comment is the output value. This is no different from the array we see in other languages. This a[' 2 '] = 12 because ' 2 ' is converted to an integer and then converted to a string or ' 2 ', so it's like a[2]. But in other languages, we use arrays to define a fixed size array, right? It's not like it's here, and it says the index range of the array. So, why? The simple answer is that the array here is the object, which is the object in JS. This differs from other languages (except for functions such as Python), specifically where different, I did not delve into, also said not very clear, I understand that, C/java and other languages to define the array, is in the memory of a fixed size of the area, there is a pointer to store the area's first address. And JS is not like this, as mentioned earlier, the array is the object, the key value to the structure, so I think the array in JS is a hash of the way to store elements, the memory between the elements is not necessarily continuous. But I can't find the way to see the JS variable memory address, so I'm not sure about it. But that's not the point of this article.
We put emphasis on the index, said the scope of the index, but some students may have tried, is a[-1] = 2, or a[4294967296] = 10; This statement is not a problem. Right, this is not a mistake, this is the normal statement, of course, there is no problem. But the question is not that the index must be an integer from 0 to 4294967295? Yes, that's true, too. Where is the question?
Put two screenshots of the Firefox console first:
Did you find anything wrong? When we add elements using the normal index, when you print an array, the elements that you add are printed, and when you add an element with an unhealthy index, there is no added element in the printed array, but you see the array object on the right, and all the added elements are a lot. Look at a picture again, add a few more elements this time, and add an attribute to the array (note the index of the element on the right array object):
I don't know if you found out. On the right, several of the above are numeric indices that can be printed when the array is printed, while the following are attributes, and the print array does not print the properties! In other words, a[-2] = 2; This statement, this-2 is the key of the property, not the key of the special numeric index,-2 is considered a string when it is coerced into a positive integer, so this-2 and 4294967296 and ' name ' are all keys to an attribute of the array! So the negative index or out of range index (which should be said to be the key of the attribute) is legitimate, and they are all normal string keys.
One problem here is that since 2 is a normal property key, one might say why the value of A.-2 or a. '-2 ' access-2 will be an error, and a[-2 will not complain? Yes, why? In front of the object, there is a bold remark: the object's properties or methods can be accessed using square brackets or dots, where the use of square brackets to enclose quotes, the use of the dot can only be used when the property name is a valid variable name. That is, the attribute does not contain any whitespace hyphens and does not begin with a number. So,-2 of the properties of this key, can not be accessed by the point number of the way!
There's one other little problem, is the square bracket, and when we want to access the array's Name property, we need to do this: a[' name ', that is, the name is wrapped in quotes, and-2 is the key to the property like name, why-2 can be wrapped in quotes without (or can)? In fact, all the characters in square brackets are treated as an expression, a simple number-2 is a valid expression, but if name is not defined as a variable name, name is not a valid expression, and likewise x^b&c is not legal because it is treated as a variable x, B, C composed of some kind of expression, but X, B, C is not the variable is not sure, and the symbol is not necessarily JS support, so a[name] problem in name, rather than [] on. If it's not easy to understand, you can think of name as X+y, and when X and Y are not defined as variables, x+y this expression must be problematic, right? That A[x+y] is going to be a problem, right? and a[' x+y ' is no problem, because ' x+y ' is a string.
In addition, JS, the variable name can be a number, letters, underscores any combination of, where the number can not be placed in the starting position. And the object's property key name should be loose some, legitimate can not use quotation marks, illegal quotes wrapped in quotation marks.
Well, said almost, summed up: The article first briefly introduced the next object, and then said that the array is also an object, and finally explained the next some questions, and then summed up.
The reason for writing this article is that I saw a JS tutorial in Weibo yesterday, the explanation of the log group produced a question, then commented the word number of more than 140, so I looked up the data alone. The goal is to make yourself understand, but also willing to help students to learn JS. Array range and index conversion there are references to "Speaking Javascript", and other places are their own understanding and views.
Finally, thanks to watch, because it is written two times, may be a bit messy, some places are not chaotic but the context is complete, and some of the context is complete but too much nonsense, in short, so it. See you in the next article.