With (object)
Statements
Parameters
Object
New default object.
Statements
One or more statements. An object is the default object of the statement.
Description
The with statement is usually used to shorten the amount of code that must be written in a specific situation. In the following example, pay attention to the reuse of Math:
X = Math. cos (3 * Math. PI) + Math. sin (Math. LN10)
Y = Math. tan (14 * Math. E)
When the with statement is used, the Code becomes shorter and easier to read:
Copy codeThe Code is as follows:
With (Math ){
X = cos (3 * PI) + sin (LN10)
Y = tan (14 * E)
}
With (),,,;
With (document) write (fileSize), write ('<br>' + lastModified)
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Avoid using JavaScript
JavaScript allows you to use the with keyword to specify a series of attributes or methods of an object. For example, there is a piece of code:
Copy codeThe Code is as follows:
Var x = document. body. scrollLeft;
Document. write ('text1 ');
Document. write ('text2 ');
Document. write ('text3 ');
If you use with, you can write as follows:
Copy codeThe Code is as follows:
With document {
Var x = body. scrollLeft;
Write ('text1 ');
Write ('text2 ');
Write ('text3 ');
}
However, this seemingly streamlined code method will increase the execution time of JavaScript much. Because it checks whether each variable in braces is the property or method of the object.
Therefore, if you want to streamline the code, you can write it like this to avoid using.
Copy codeThe Code is as follows:
Var d = document;
Var x = d. body. scrollLeft;
D. write ('text1 ');
D. write ('text2 ');
D. write ('text3 ');