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.