The difference of use strict to duplicate attribute processing in different JS environments

Source: Internet
Author: User

I changed a bug yesterday. The property name is repeated, which leads to a very interesting little bug. The result of a bug is the code of a JS object declaration, which is equivalent in structure to the following code.

var fn = function () {" use strict ' ; var obj = {A: 1 , A: 2  // Because the declared property is more, the property added later is accidentally repeated with the existing property }; return  obj};fn (); 

At that time, I tested the program in the chrome of the PC, ran it, and tested it on the Android, no problem. But in the iphone, but the error, resulting in the page does not render properly. It is easy to locate the wrong code by using Safari to connect to the phone and find this interesting problem: The strict mode has different processing strategies for repeating attributes in different JS running environments . Let's take a look at the execution of this code in each of the operating environments

    • Chrome
    • Safari
    • Firefox
    • Nodejs

From the above experimental results can be seen, even in strict mode, the various operating environment to the partial details of the processing is not the same. In strict mode, Safari and Nodejs object literals prohibit duplicate attribute declarations, while Chrome and Firefox do not have this limitation. The same scripting engine that chrome and Nodejs supposedly use should be consistent, and the difference is a bit of a hassle. So even if the code running in strict mode is not 100% insured, it is necessary to do more testing .

The difference of use strict to duplicate attribute processing in different JS environments

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.