with (object)
statements
parameters
object
New default object.
statements
One or more statements, object is the default object of the statement.
Explanation
The with statement is often used to shorten the amount of code that must be written in a specific situation. In the following example, note the repeated use of Math:
x = Math.cos(3 * Math.PI) Math.sin(Math.LN10)
y = Math.tan(14 * Math.E)
When using the with statement, the code becomes shorter and more readable:
with (Math){
x = cos(3 * PI) sin (LN10)
y = tan(14 * E)
}
with(),,,;
with(document)write(fileSize),write('
' lastModified)
]
Avoid using JavaScript With
JavaScript allows using the with keyword to specify a series of properties or methods of an object. For example, there is a piece of code like this:
The code is as follows:
var x = document.body .scrollLeft;
document.write('text1');
document.write('text2');
document.write('text3');
If used With, you can write like this:
The 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 a lot. Because it will check whether each variable within the curly brackets is a property or method of this object.
So, if you want to simplify the code, you can write it like this to avoid using with.
The code is as follows:
var d = document;
var x = d .body.scrollLeft;
d.write('text1');
d.write('text2');
d.write('text3');
<script>
with(document)write(fileSize)
,write('<br>'+lastModified)
,write('<br>'+location)
,write('<br>'+protocol)
,write('<br>'+location.host)
</script>