2. After the syntax analysis is passed, the js engine will execute the code. An error that occurs during execution is called a running error
Different engines handle these two errors with different prompts. as follows:
var p = {name:"Jack",age:33,};//Note that there is a comma after 33
= function() {return "name: " + + ", age: " + };
(p);
alert(p);//Name: Jack, age 33<br>
Test under firefox, the engine will ignore the comma after 33, and can pass the syntax check, and there will be no errors during the execution period.
In the test under IE6/7, the syntax analysis period reported an error, and of course it would not enter the execution period.
However, this problem has been fixed under IE8 and there will be no errors. Other browsers will not report errors.
To summarize: This error is difficult to detect. Often, you accidentally add a comma, or define an object or array with many attributes and then delete some of them, and accidentally leave an extra comma.
//Irregular writing
var p = {name:"Jack",age:33,};
var ary = ["one","two","three",];
// Standardized writing
var p = {name:"Jack",age:33};
var ary = ["one","two","three"];
In addition, you may also encounter this problem when defining the direct quantity of an array, such as an extra comma at the end of the array
var ary = [1,2,];
();
IE6/7/8 output length is 3, and IE9 and other browsers are 2. ECMAScript 5 11.1.4 There is a paragraph that states that the last comma should be ignored. But the specification was not implemented until IE9. Other browsers are fine.
ECMAScript 5 11.1.4 writes:
Array elements may be elided at the beginning, middle or end of the element list. Whenever a comma in the element list is not preceded by an AssignmentExpression (., a comma at the beginning or after another comma), the missing array element contributes to the length of the Array and increases the index of subsequent elements. Elided array elements are not defined. If an element is elided at the end of an array, that element does not contribute to the length of the Array.
Someone once used this feature of array to create the so-called "The Shortest IE Judgment in the World"
var ie = !-[1,];
alert(ie);
But it was terminated under IE9. Don't use this bug to judge the browser.
JSON
In JSON format, commas are separators between multiple attribute key-value pairs, for example:
var json = { id: 1, name: 'heero' };
But when programming, it is easy to add to the slightest.Comma is added after the last pair of key-value pairs:
var json = { id: 1, name: 'heero', };
In this case, IE6 and 7 will report errors, but IE8 and other browsers have no problems.
Array
In an array, a comma is a separator between elements, for example:
var arr = [1, 2, 3];
Similarly, we may be carelessAdd a comma after the last element:
var arr = [1, 2, 3,];
Intuitively, this should be the wrong syntax. But in reality, 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 < ; i++) { alert(arr[i]); }
On browsers other than IE, 1, 2, and 3 are output in sequence; but on IE browsers, 1, 2, 3, and undefined are output in sequence. Obviously,IE adds an undefined element after that extra comma。
Consider another situation, the extra comma is not at the end, but in the middle:
var arr = [1, 2,, 3,];
for (var i = 0; i < ; i++) { alert(arr[i]); }
All browsers output 1, 2, undefined, and 3. It can be seen that the processing method is the same, that is,Insert an undefined element before the extra comma。