JavaScript strict mode with with keyword introduction

  • 2020-03-30 01:38:30
  • OfStack

Released in December 2009, ECMAScript ECMAScript 5, this distance on a version of the ECMAScript standard 3 have been issued for 10 years, during which the JavaScript although big line in web programming, ECMAScript 4 but eventually because of stakeholders of the complexity of the major manufacturers and organizations in this language (i.e., whether to increase a lot of features to extend the functionality of ECMAScript) differences and die, makes the ECMAScript new standards greatly lagged behind the practice of programming. ECMAScript 5 is less ambitious in its goals, and aside from the new support for JSON and more comprehensive control of reflection, one major improvement is the introduction of "strict mode." In this pattern, the syntax of ECMAScript becomes more rigid, disallowing many of the usual error-prone code, including mandatory variable declarations and disallowed with statements. Using this mode is as simple as adding "use strict" to the first line of a script file or function. So that's one line of string.

As I later learned, in 2010 I wrote a short article on the defects of the with keyword, which is attached below.

A long time ago, a road was named after him in his hometown in honor of him. Ma liang did not refuse, but put forward four words of opinion. Years later, a stranger came here and stopped a local man on the road to ask the way.

What's the way, please?

Yeah, what's this?

Don't you know?

I'm the guy here. I don't know.

So what's this road?

You know what else to ask.

I just don't know what the road is.

Didn't I tell you the way?

Can you say that again?


Later, the local remembered the good advice and suddenly realized. What ma liang said is -- don't use short names.

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 Clearly by the shop's CCTV.

The question is when Tom said his dream, he is

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

The right answer is B) and C).
The body of the

The above two ancient and modern examples of Chinese and foreign abbreviations sometimes cause ambiguity. This is also true in Javascript. Sometimes it can be troublesome to repeat 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 readability. So Javascript provides the with statement. The above example can be rewritten as:
with (objectWithLongName1){ 






This saves a lot of typing and makes the structure of the program clearer. But this shorthand introduces ambiguity, how do we know the names in braces, which are the properties and methods of objectWithLongName1, and which are external variables and functions. The rule of Javascript parsing is to look up the properties of these names on objectWithLongName1, and if they are not found, consider them external variables. This is what the code says:


objectWithLongName1.property1=objectWithLongName1.value1; //May 1


objectWithLongName1.property1=value1;//May 2




property1=objectWithLongName1.value1; //May 3


property1=value1;//May 4



We want one of these four possibilities, but if we're not careful, the program will execute on the other. Moreover, this is a very difficult way to write a program for the reader. On the other hand, with the Javascript interpreter, this uncertainty also affects the performance of the language.

In fact, as long as a small improvement, can eliminate these defects. We can make an intuitive distinction between attributes and external variables by omitting the attributes of an object, as many other languages do. Our first example would look like this:
with (objectWithLongName1){ 






Until Javascript does this, take the lesser of two evils and try to avoid using the with statement. We can still work around it.
var o1= objectWithLongName1; 





Or in cases like this:

ObjectWithLongName1. Propty1 = objectWithLongName2. Propty1;

ObjectWithLongName1. Propty2 = objectWithLongName2. Propty2;


ObjectWithLongName1. Propty10 = objectWithLongName2. Propty10;

It can be written as:
(function(o1, o2, pl){ 


})( objectWithLongName1,objectWithLongName2, [ ' propty1',  ' propty2',  ...  ,  ' propty10']); 

Related articles: