The usage and introduction of with in JavaScript

Source: Internet
Author: User

Let's take a look at the basic usage of with

With (object)
Statements
Parameters
Object
The new default object.
Statements
One or more statements, object is the default object for the statement.
description
The WITH statement is typically used to shorten the amount of code that must be written in a particular case. In the following example, note the repeated use of Math:

The code is as follows Copy Code
x = Math.Cos (3 * Math.PI) + Math.sin (MATH.LN10)
y = Math.tan (* math.e)

When you use the WITH statement, the code becomes shorter and easier to read:

The code is as follows Copy Code

With (Math) {
x = cos (3 * PI) + sin (LN10)
y = tan (E)
}


we're going to analyze if it's only read-only.


When the execution stream enters any of the following statements, the scope chain is extended:


* 1) Try-catch the CATCH block of the statement
* 2) With statement

The two statements add a variable object to the front end of the scope chain. For with, the variable object contains a variable declaration of all the properties and methods of the specified object, and for catch, the variable object contains the declaration of the thrown error object. These scalar objects are read-only, so the variables declared in the with and catch statements are added to the variable object in the execution environment.
This is the phrase in the second edition of JavaScript Advanced programming. However, with the extended scope, is the scalar object really read-only? Or do I understand that the scalar is not the right thing?
No nonsense, send your own test code can be:

The code is as follows Copy Code

Will jump
(function () {
With (location) {
href= ' http://www.111cn.net ';
}
})();


B
(function () {
var obj = {A: ' A '};

With (obj) {
A = ' B ';
}

alert (OBJ.A);
})();

All right, we can test it out.

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.