Home > Web Front-end > JS Tutorial > Introduction to JavaScript's strict mode and with keyword_Basic knowledge

Introduction to JavaScript's strict mode and with keyword_Basic knowledge

WBOY
Release: 2016-05-16 17:01:09
Original
1191 people have browsed it

In December 2009, ECMAScript released ECMAScript 5. It has been ten years since the release of the previous version of the ECMAScript 3 standard. During this period, although JavaScript was popular in web programming, ECMAScript 4 was eventually adopted by major manufacturers and organizations with relevant interests. The complexity of the language (that is, whether to add a large number of features to expand the functionality of ECMAScript) was stymied by disagreements, making the formulation of new ECMAScript standards far behind programming practices. ECMAScript 5 is less ambitious in its goals. In addition to adding support for JSON and more comprehensive control of reflection, an important improvement is the introduction of "strict mode". In this mode, the syntax of ECMAScript becomes more strict, so that many common error-prone codes are no longer allowed, including mandatory variable declarations and disallowed with statements. Adopting this mode is very simple, just add "use strict"; such a line of string to the first line of the script file or function.

The author later learned that in 2010, he wrote a small article discussing the shortcomings of the with keyword, which is attached below.
Wedge

A long time ago, the magic pen Ma Liang’s hometown wanted to name a road after him in order to commemorate him. Ma Liang did not refuse, but put forward four words of advice. Many years later, a foreigner came here and stopped a local on this road to ask for directions.

Excuse me, is this a holy road?

Yes, this is Shenma Road.

Don’t you know either?

I am the person here, how could I not know.

Then is this a holy road?

Why do you ask after you know.

I just don’t know this is a holy road.

Then didn’t I already tell you that this is a holy road?

Can you say again that this is a holy road?

......

Afterwards, the local man remembered the opinion of Ma Liang, the magic pen, and suddenly realized. What Ma Liang said is - 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 he 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) unambiguous

The right answer is B) and C). . This also exists in Javascript. Sometimes it is troublesome to repeatedly reference a variable with a long name, such as:

objectWithLongName1.propty1=value1;

objectWithLongName1.propty2=value2;

objectWithLongName1. propty3=value3;

objectWithLongName1.method1();

But a clear name is very important for the readability of the program. So Javascript provides the with statement. The above example can be rewritten as:



Copy code The code is as follows: with (objectWithLongName1){
propty1=value1;

propty2=value2;

propty3=value3;

method1();

}


This saves a lot of time typing on the keyboard, and the structure of the program becomes clearer. But such abbreviation introduces ambiguity, how do we know the name inside the curly brackets, which are the properties and methods of objectWithLongName1, and which are the external variables and functions. The parsing rule of Javascript is to first look for the properties of these names on objectWithLongName1. If not found, they are considered to be external variables. The code description is like this:


Copy the code The code is as follows: if(objectWithLongName1.property1! ==undefined){
if(objectWithLongName1.value1!==undefined){

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

}else{

objectWithLongName1.property1=value1;//possible 2

}

}else{

if(objectWithLongName1.value1!==undefined){

property1=objectWithLongName1.value1; //Possibly 3

}else{

property1=value1; //Possibly 4

}

}


What we hope is one of these four possibilities, but if we are not careful, the program execution will be another possibility. Moreover, this way of writing is also very difficult for readers of the program to understand. On the other hand, for the Javascript interpreter, this uncertainty also affects the performance of the language.
In fact, as long as a small improvement is needed, these defects can be eliminated. We can add a period before the properties of the omitted object, thus adding an intuitive distinction between properties and external variables. Many other languages ​​do this. Our initial example will look like this:



Copy the code The code will look like this:

with (objectWithLongName1){

.propty1=value1;

.propty2=value2;

.propty3=value3;

.method1();

}

Before Javascript makes such improvements, the lesser of two evils is to avoid using the with statement. We can still use some workarounds.
Copy code The code is as follows:

var o1= objectWithLongName1;

o1 .propty1=value1;

o1.propty2=value2;

o1.propty3=value3;

o1.method1();

Or for a case like this:

objectWithLongName1.propty1= objectWithLongName2.propty1;

objectWithLongName1.propty2= objectWithLongName2.propty2; propty10= objectWithLongName2.propty10;

can be written as:




Copy code
The code is as follows: (function(o1, o2, pl){
pl.forEach(function(item){o1[item]=o2[item];});

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template