1, JS engine in the code load in the syntax analysis, if the JS write is not standardized grammar analysis pass. The error of this time is called a grammatical error
2, the grammar analysis passed, the JS engine will execute the code. Errors that occur during execution are known as run errors
Different engines handle these 2 kinds of errors with hints that are not quite the same. As follows:
Copy Code code as follows:
var p = {name: "Jack", age:33,};//notice a comma after 33
p.tostring = function () {return "name:" +this.name + ", Age:" + this.age};
Console.log (P);
Alert (P);//Name: Jack, age 33<br>
Firefox under the test, the engine will ignore the 33 comma, you can pass the grammar check, in the implementation period will not error
IE6/7 under the test, parsing period on the error, of course, will not enter the implementation period.
However, the problem has been fixed under the IE8, there will be no error. Other browsers do not have an error.
Summary: This error is difficult to find, often by accidentally adding a comma, or defining a lot of attributes of an object or an array will then delete some of them and accidentally left an extra comma.
Copy Code code as follows:
Non-standard wording
var p = {name: "Jack", Age:33,};
var ary = ["One", "two", "three",];
The wording of the specification
var p = {name: "Jack", age:33};
var ary = ["One", "two", "three"];
In addition, you may also encounter this problem when defining the direct amount of an array, which is the last comma
Copy Code code as follows:
var ary = [1, 2,];
Console.log (ary.length);
IE6/7/8 output length is 3,IE9 and other browsers are 2. ECMAScript 5 11.1.4 One of the paragraphs shows that the last comma should be ignored. But the specification was not implemented until IE9. Other browsers are fine.
ECMAScript 5 11.1.4 wrote:
Array elements May is elided at the beginning and middle or end of the element list. Whenever a comma in the element list isn't preceded by a assignmentexpression (i.e., a comma at the beginning or after a nother comma), the missing array element contributes to the length of the array and increases the index of subsequent Ents. elided array elements are not defined. The If an element is elided at the ' End of ' an array, which element does not contribute to the length of the array.
Someone used this feature of the array to create the so-called "world's shortest ie judgment"
Copy Code code as follows:
var ie =!-[1,];
alert (IE);
But it was terminated under the IE9. Don't use this bug to judge your browser.
Json
In JSON format, a comma is a separator between multiple property key value pairs, for example:
var json = { id: 1, name: 'heero' };
But in programming, it is easy to gild the lily, after the last pair of key value pairs also added a comma :
var json = { id: 1, name: 'heero', };
In this case, IE6, 7 will complain, but IE8 and other browsers are fine.
Array
In the array, the comma is the delimiter between elements, for example:
var arr = [1, 2, 3];
Similarly, it is possible to accidentally add a comma after the last element :
var arr = [1, 2, 3,];
Intuitively, this should be the wrong syntax. In practice, however, all browsers are compatible with this situation, including IE6. Consider a sample code like this:
var arr = [1, 2, 3,];
for (var i = 0; i < arr.length; i++) { alert(arr[i]); }
In browsers other than IE, the output is 1, 2, 3 in turn, but in IE browser, the output is 1, 2, 3, undefined. Obviously,ie adds a undefined element to the extra comma .
Consider the other case where the extra comma is not at the end, but in the middle:
var arr = [1, 2,, 3,];
for (var i = 0; i < arr.length; i++) { alert(arr[i]); }
All browsers output 1, 2, Undefined, 3, visible processing is the same, that is insert a undefined element before the extra comma.