Processing of unnecessary commas by browsers when js defines objects or arrays directly (json)

Source: Internet
Author: User

1. The js engine performs syntax analysis when the code is loaded. If the JavaScript code is not standardized, the syntax analysis will fail. This error is called a syntax error.
2. After the syntax analysis is passed, the js engine executes the code. An error occurred during execution is called a running error.

Different engines have different prompts for handling these two types of errors. As follows:
Copy codeThe Code is as follows:
Var p = {name: "Jack", age: 33,}; // note that there is a comma after 33
P. toString = function () {return "name:" + this. name + ", age:" + this. age };
Console. log (p );
Alert (p); // name: Jack, age 33 <br>

For tests in firefox, the engine ignores the comma after 33 and can pass the syntax check. No error is reported during the execution period.
In IE6/7 testing, an error is reported during the syntax analysis period, and of course it will not enter the execution period.
However, this problem has been fixed in IE8 and no error is reported. No error is reported in other browsers.

To sum up, this error is hard to find, and a comma is often added when you are not careful, you can also define an object or array with many attributes and then delete some of them, leaving unnecessary commas.
Copy codeThe Code is as follows:
// Nonstandard Writing Method
Var p = {name: "Jack", age: 33 ,};
Var ary = ["one", "two", "three",];
// Standard writing
Var p = {name: "Jack", age: 33 };
Var ary = ["one", "two", "three"];

In addition, this problem may also occur when defining the direct amount of arrays. For example, the array has a comma at the end.
Copy codeThe Code is as follows:
Var ary = [1, 2,];
Console. log (ary. length );

In IE6/7/8, the output length is 3, and in IE9 and other browsers it is 2. ECMAScript 5 11.1.4 contains a section indicating that the last comma should be ignored. However, this specification is not implemented until IE9. Other browsers are fine.

ECMAScript 5 11.1.4 wrote:

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 (I. e ., 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 used this array feature to create the so-called "world's shortest IE judgment"
Copy codeThe Code is as follows:
Var ie =! -[1,];
Alert (ie );

However, IE9 is terminated. Do not use this Bug to determine the browser.

JSON

In JSON format, comma is the delimiter between multiple attribute key-value pairs. For example:

var json = { id: 1, name: 'heero' };

However, when programming, it is easy to draw a snake,A comma is added after the last key-value pair.:

var json = { id: 1, name: 'heero', };

In this case, IE6 and 7 will report an error, but IE8 and other browsers are OK.

Array

In an array, commas are separators between elements, for example:

var arr = [1, 2, 3];

Likewise, we may be not careful.Add a comma after the last element.:

var arr = [1, 2, 3,];

Intuitively, this should be a wrong syntax. But in fact, all browsers are compatible with this situation, including IE6. Consider the following sample code:

var arr = [1, 2, 3,];
for (var i = 0; i < arr.length; i++) { alert(arr[i]); }

In browsers other than IE, 1, 2, and 3 are output in sequence. in IE, 1, 2, 3, and undefined are output in sequence. Obviously,IE adds an undefined element after the extra comma..

In another case, 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, and 3. the processing method is the same, that isInsert an undefined element before the redundant comma..

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.