Simplified with coding in javascript

Source: Internet
Author: User

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 ');

Related Article

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.