JavaScript Strict mode with keyword Introduction _ Basics

Source: Internet
Author: User
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.
Wedge

Long, long ago, Pen Ma's hometown in honor of his name will be a road. 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 the road of God?

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.

So I already told you this is God's road?

Can you say again this is the road of God?

......

Later, the locals think of the 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 of the time to identify and arrest Tom because who is captured very by the "shop" s CCTV.

The question is when Tom said his dream

A) ambitious B) not ambitious C ambiguous D) unambiguous

The right answer is B) and C.
Body

The above two examples in ancient and modern times can cause ambiguity. This also exists in JavaScript. Sometimes it is troublesome to refer repeatedly 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:
Copy Code code as follows:

With (objectWithLongName1) {

propty1=value1;

Propty2=value2;

Propty3=value3;

Method1 ();

}

This saves a lot of time knocking on the keyboard, 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 find the properties of these names on the objectWithLongName1 first and, if not, to consider them to be external variables. This is the case with code:
Copy Code code as follows:

if (objectwithlongname1.property1!==undefined) {

if (objectwithlongname1.value1!==undefined) {

objectwithlongname1.property1=objectwithlongname1.value1; Maybe 1

}else{

objectwithlongname1.property1=value1;//may be 2

}

}else{

if (objectwithlongname1.value1!==undefined) {

property1=objectwithlongname1.value1; Maybe 3

}else{

property1=value1;//may be 4

}

}

We want one of these four possibilities, but carelessly, the program executes 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 would be this:
Copy Code code as follows:

With (objectWithLongName1) {

. propty1=value1;

. propty2=value2;

. propty3=value3;

. METHOD1 ();

}

Before JavaScript makes such an improvement, the two evils are lighter, so try to avoid using the WITH statement. We can still adopt some alternative methods.
Copy Code code as follows:

var o1= objectWithLongName1;

o1.propty1=value1;

O1.propty2=value2;

O1.propty3=value3;

O1.method1 ();

Or for a situation like this:

Objectwithlongname1.propty1= objectwithlongname2.propty1;

Objectwithlongname1.propty2= Objectwithlongname2.propty2;

......

Objectwithlongname1.propty10= Objectwithlongname2.propty10;

Can be written as:
Copy Code 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.