Home > Web Front-end > JS Tutorial > Simplified code writing method of with under javascript_javascript skills

Simplified code writing method of with under javascript_javascript skills

WBOY
Release: 2016-05-16 19:06:08
Original
1160 people have browsed it

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:

Copy code The code is as follows :

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


with(),,,;
with(document)write(fileSize),write('
' lastModified)

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]
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>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template