The concept of the package or namespace should not be unfamiliar to the students who have used Java and. NET, and because of this concept, the simplicity and readability of the code is guaranteed. I do not know how JavaScript design at the beginning of how to locate the With statement, the individual feel that they have a certain degree of similarity. Such as:
Copy Code code as follows:
Apple.banana.candy.dog.egg.fog.god.huh.index = 0;
DoSomething (Apple.banana.candy.dog.egg.fog.god.huh.index);
The With statement can be written as the following code.
Copy Code code as follows:
With (Apple.banana.candy.dog.egg.fog.god.huh) {
c = 0;
DoSomething (index);
}
It looks wonderful, but it has a fatal flaw. Let's take some small tests.
1. Modifying numeric values within the WITH statement with internal variables
Copy Code code as follows:
var root = {
Branch: {
Node:1
}
};
With (Root.branch) {
node = 0;
Show 0, right!
alert (node);
}
Show 0, right!
alert (Root.branch.node);
2. Modifying values through object nodes within the WITH statement
Copy Code code as follows:
var root = {
Branch: {
Node:1
}
};
With (Root.branch) {
Root.branch.node = 0;
Show 0, right!
alert (node);
}
Show 0, right!
alert (Root.branch.node);
After testing 1 and 2, at first glance it's OK, but ... Take a look at Test 3.
3. Modifying values within the WITH statement through the object parent node
Copy Code code as follows:
var root = {
Branch: {
Node:1
}
};
With (Root.branch) {
Root.branch = {
node:0
};
Show 1, Error!
alert (node);
}
Show 0, right!
alert (Root.branch.node);
The test 3 above shows that the node parent node inside the With statement is modified and does not synchronize to the node itself. In other words, there is no guarantee of consistency between internal and external values. This is a potentially highly hidden bug in the project.
So what do we do? Accept that long sequence of visits, or is there another way?
Method is available. We can invoke the node object by referencing the parent node by an alias, such as:
Copy Code code as follows:
var root = {
Branch: {
Node:1
}
};
var quote = Root.branch;
Quote.node = 0;
Show 0, right!
alert (Root.branch.node);
I believe that very few people will use the WITH statement, and not many people know the keyword, but I think this is a problematic statement, should not be used, so write a small text to record.