The use of JavaScript with statements

Source: Internet
Author: User

The WITH keyword for web effects, with statements intended to provide a namespace-style sketch for progressive object access. That is, in the specified code area, the object is invoked directly through the node name.


With (object) {
Code
}

<title>a Simple page</title>
<script language= "JavaScript" >
<!--
INP1 = 1;
INP2 = 2;
INP3 = 3;
With (Math)
{
Alert ("The largest number entered was" + MAX (INP1, INP2, INP3));
Alert ("The smallest number entered was" + min (inp1, INP2, INP3));
}
-->
</script>
<body>

</body>

Example Two

<script language= "JavaScript" >
<!--
var myNum = 25;

var mye, Mysin, mysqrt;

With (Math) {
Mye = E;
Mysqrt = sqrt (myNum)
Document.writeln (MYSQRT);
Mysin = sin (pi/mynum);
document.write (Mysin);
}
-->
</script>

Using the object parent node to modify the value inside the WITH statement

var root = {
Branch: {
Node:1
}
};

With (Root.branch) {
Root.branch = {
node:0
};
Show 1, Error!
alert (node);
}
Show 0, right!
alert (Root.branch.node);

Note: The node parent node inside the With statement is modified and is not synchronized 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.

Related Article

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.