How to use the With statement on JavaScript _javascript tips

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

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.