JavaScript statements -with statement

with statement

Action with the code statement is provided to the scope of a particular object. with statement syntax is as follows:

with(expresssion) statement;

Defined with the main purpose is to simplify the preparation of the same object many times the work, as shown in the following example:

var qs = location.search.substring(1);
var hostname = location.hostname;
var url = location.href;

The above location of the object code contains several lines, if with statement, the above code can be rewritten as follows:

with(location) {
    var qs = search.substring(1);
    var hostname = hostname;
    var url = href;
}

In this rewrite example, with statement associated with the location object. This means that the code block with the statement, each of the first variable is considered to be a local variable, and if it can not find the definition of the variable in a local variable, it will check whether there are attributes of the same name location object. If you find a property of the same name, it will be subject to the value of the property as the location value of the variable.

Under strict mode does not allow the use of the with statement, or as a syntax error.

Due to extensive use of the with statement can result in performance degradation, but also difficult to code debugging, so the development of large applications, not recommended for use with statement.

Guess you like

Origin www.cnblogs.com/cherishSmile/p/11102467.html