JavaScript strict mode and the WITH keyword introduction

Source: Internet
Author: User

  This article mainly introduces the JavaScript strict mode and with keyword, need friends can refer to the following

In December 2009, ECMAScript released ECMAScript 5, which is 10 years from the previous version of the ECMAScript 3 standard, which, while JavaScript is great for Web programming, ECMAScript 4 but ultimately because of the interests of the major manufacturers and organizations in this language complexity (that is, to increase the number of features to expand the function of ECMAScript) on the difference between the death, making the ECMAScript new standard of the development of significantly behind the practice of programming. ECMAScript 5 is less ambitious on the target, eliminating the addition of a more comprehensive control of support and reflection for JSON, an important improvement is the introduction of "strict Mode" (Strict mode). In this mode, the syntax of the ECMAScript becomes stricter, making it possible that many of the usual error-prone codes are no longer allowed, including enforcing variable declarations and not allowing with statements. This pattern is simple, as long as the "use strict" is added to the first line of the script file or function;     I know that 2010 has also written a small article to discuss the defect with the keyword, attached below.   Wedges     Long long ago, pen Ma's hometown was named after him in honor of his name. Ma Liang did not refuse, but put forward four words of advice. Years later, a foreign man came here to stop a local on the road and ask for directions.     Excuse me, is this God's road?     Yes, this is the road of God.     You don't know that, do you?     I am this person, how can not know.     So this is God's road?     You know what else to ask.     I just didn't know it was God's road.     Well, didn't I tell you this is God's road?     Can you say again this is the road of God?     ......    Later, the local people think of Pen Ma Liang's opinion, suddenly dawned. Ma Liang said--don't use abbreviations.     A question    one day Tom said to Wang Er, his Chinese friend, "I have a dream." I want to show myself on CCTV. " The next day, Tom broke into a NEIghbor Shop. The police didn ' t take much time to identify and arrest Tom because him was captured very clearly by the shop ' s cctv.    The question is when Tom said his dream, he is    A) ambitious B) not ambitious C ambiguous D) Unambiguo us    The right answer are B) and C .  text     Above two examples of ancient and modern illustration sometimes cause ambiguity. This also exists in JavaScript. Sometimes it's troublesome to refer to a variable with a long name, such as:    objectwithlongname1.propty1=value1;    objectwithlongname1.propty2=value2;    objectwithlongname1.propty3=value3;    OBJECTWITHLONGNAME1.METHOD1 ();    But a clear name is important for the readability of the program. So JavaScript provides a with statement. The above example can be rewritten as:    code as follows: With (objectWithLongName1) {    propty1=value1;    propty2=value2;     propty3=value3;    method1 ();   }    This eliminates a lot of keystrokes and the structure of the program becomes clearer. But such shorthand introduces ambiguity, how we know the names within braces, which are objectWithLongName1 properties and methods, and which are external variables and functions. The parsing rule for JavaScript is to look up the properties of these names on the objectWithLongName1 first, and if they are not, consider them externalVariable. This is how the code is written.:    code is as follows: if (objectwithlongname1.property1!==undefined) {    if ( objectwithlongname1.value1!==undefined) {    objectwithlongname1.property1=objectwithlongname1.value1; /May 1   }else{    objectwithlongname1.property1=value1;//may 2   }   }else{     if (objectwithlongname1.value1!==undefined) {    property1=objectwithlongname1.value1;//maybe 3    }else{    property1=value1;//may 4   }   }    We want one of these four possibilities, But accidentally, the program executes is another possibility. Moreover, this kind of writing is also very difficult to solve for the reader of the program. On the other hand, for JavaScript interpreters, this uncertainty also affects the performance of the language.     In fact, as long as a small improvement, you can eliminate these deficiencies. We can add an intuitive distinction between attributes and external variables by adding a point number to the attribute that omits the object, and there are many other languages that do so. Our initial example will become like this:    code is as follows: With (objectWithLongName1) {    .propty1=value1;    propty2= value2;    .propty3=value3;    method1 ();   }    before JavaScript makes such an improvement, the two evils take their light , try to avoid using the WITH statement. We can still adopt some alternative methods.    Code as follows: Var o1= objectwithlongname1;    o1.propty1=value1;    o1.propty2=value2;    o1.propty3=value3;    O1.METHOD1 ();    or for such a situation:    objectwithlongname1.propty1= objectwithlongname2.propty1;    objectwithlongname1.propty2= objectwithlongname2.propty2;    ...     objectwithlongname1.propty10= objectwithlongname2.propty10;    can be written as:  code as follows: (Function (O1 , O2, PL) {    pl.foreach (function (item) {O1[item]=o2[item];});    }) (Objectwithlongname1,objectwithlongname2, [' propty1 ', ' propty2 ', ..., ' propty10 ']); 
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.