In December 2009, ECMAScript released ECMAScript 5, which has been released for the last version of the ECMAScript 3 standard for the previous version. Although JavaScript is on Web programming, ECMAScript 4 is eventually eventually due to the major manufacturers and organizations related to interests. The complexity of this language (that is, whether to increase a large number of characteristics to expand the function of ECMAScript) and died, making the formulation of new ECMAScript standards that greatly lag behind programming. ECMAScript 5 is not so ambitious in terms of goals. Except for more comprehensive control of support and reflection of JSON, an important improvement is to introduce "Strict Mode". In this mode, the syntax of ECMAScript has become more stringent, making many commonly -made easy -to -prone code no longer allowed, including mandatory variable declarations and non -allowing With statements. This mode is very simple. As long as you add "Use Strict" in the first line of the script file or function; then a line of string can be used.
I later learned that in 2010, I also wrote a short article to discuss the defects of with keywords, with the following.
wedge
A long time ago, the hometown of God Ma Liang's hometown was named after his name. Ma Liang did not resign, but made four words. Many years later, a foreigner came here and stopped a local person on this road.
Is this Shenma Road?
Yes, this is Shenma Road.
Don't you know either?
I am this person, how could I not know.
Is this Shenma Road?
You know what you ask.
I just don't know that this is Shenma Road.
Didn't I tell you this is Shenma Road?
Can you say it again?
Nympho
After that, the locals remembered the opinions of Ma Liang that year, and suddenly realized. What Ma Liang is talking about -don't use the abbreviation.
A question
One day tour 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. uch time to IDentify and arrest tom Becaue 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)
The right angr is b) and c).
text
The above two examples of ancient and modern China and foreign countries explained that the reflection sometimes causes ambiguity. This also exists in JavaScript. Sometimes it is very troublesome to regenerate a long variable, 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 With statement. The above example can be rewritten:
Copy code code as follows:
With (ObjectwithlongName1) {{
propty1 = value1;
propy2 = value2;
propy3 = value3;
Method1 ();
}
This saves a lot of kung fu on the keyboard, and the structure of the program becomes clearer. But this abbreviation introduces ambiguity. How do we know the names and methods of ObjectwithlongName1 in the Big brackets, and which are external variables and functions. The analysis rules of JavaScript are to find the attributes of these names on ObjectWithlongName1 first. If not found, they think they are external variables. This is what it is explained by code:
Copy code code as follows:
If (ObjectwithlongName1.property1! == Undefined) {
If (ObjectwithlongName1.value1! == Undefined) {
objectwithlongName1.property1 = ObjectwithlongName1.value1; // Maybe 1
} Else {
objectwithlongname1.property1 = value1; // Perhaps 2
}
} Else {
If (ObjectwithlongName1.value1! == Undefined) {
Property1 = objectwithlongname1.value1; // Perhaps 3 may
} Else {
Property1 = value1; // Perhaps 4
}
}
What we want is one of these four possibilities, but if you are not careful, the program will execute another possibility. Moreover, this way of writing is also very difficult to solve the readers of the program. On the other hand, this uncertainty also affects the performance of the language for the JavaScript interpreter.
In fact, as long as a small improvement can be removed, these defects can be removed. We can add a point number before the attributes of the object, so that there is an intuitive distinction between the attributes and the external variables, and there are many other languages that do this. Our initial examples will become like this:
Copy code code as follows:
With (ObjectwithlongName1) {{
.propty1 = value1;
.propty2 = value2;
.propty3 = value3;
.method1 ();
}
Before JavaScript made such improvements, the two harmful power is light, and we must try to avoid using the With statement as much as possible. We can still adopt some change methods.
Copy code code as follows:
var o1 = objectwithlongname1;
o1.prpty1 = value1;
o1.prpty2 = value2;
o1.prpty3 = value3;
o1.Method1 ();
Or for this situation:
objectwithlongName1.propty1 = ObjectwithlongName2.propty1;
objectwithlongName1.propty2 = ObjectwithlongName2.propty2;
Nympho
objectwithlongname1.propty10 = objectwithlongname2.propty10;
Can be written as:
Copy code code as follows:
(Function (O1, O2, PL) {
pl.Foreach (function (it) {o1 [item] = o2 [item];});
}) (ObjectwithlongName1, ObjectwithlongName2, ['Propty1', 'Propty2', ..., 'PROPTY10'));