An in-depth analysis of the understanding of the With statement in JavaScript

Source: Internet
Author: User

JavaScript has a with keyword, and the original intent of the WITH statement is to provide a namespace-style sketching method for hierarchical object access. That is, the object is called directly through the node name in the specified code area.

The function of the WITH statement is to temporarily change the scope chain and reduce the repeated input.

Its syntax structure is:

 with (object) {  //}

Let me give you a practical example:

with = "Lee King"= "Peking";   = ""; }

The traditional notation that corresponds to this is:

Document.forms[].name.value = "Lee King"= "Peking";   = "";

You can see the simplicity of the WITH statement, but it's hard to find true perfection in the code world.

The JS interpreter needs to check if the variables in the With block belong to the with object, which will make the WITH statement perform much slower and cause the JS statement to be difficult to optimize. To take into account the speed and the amount of code you can find a more eclectic solution:

var form == "Lee King"= "Peking";   = "";

Therefore, we should avoid using the WITH statement as much as possible in future efficient code development.

Tested:

var a = 123; var b = {a:321};  with  //  321}var a = 123; var b =with//  123} from the scope chain to analyze

In JavaScript, a function is also an object, and in fact, everything in JavaScript is an object. Inside the function is an internal property that is accessible only to the JavaScript engine, which contains a collection of objects in the scope at which the function was created, called the scope chain.

For example, the following code:

function Add (num1,num2) {  var sum = num1 +return  sum;}

When the function is created, it fills a global object in its scope chain, which contains all global variables, such as:

When the function is executed, an active object is created that contains all the local variables, named arguments, and this for the function, which is then pushed into the front end of the scope chain, and the object is destroyed as soon as the function finishes executing.

As you can see, global variables are pushed to the last end of the scope chain by the active object, which is why global variable access is slow!

With

In general, scope chains are only affected by the with and catch statements. When the creation is used with, the function creates a new active object and pushes it to the front end, which is the object with. This means that all local variables are in the second scope chain object, which is why you should avoid using with.

Reproduced in: https://www.jb51.net/article/84065.htm

An in-depth analysis of the understanding of the With statement in JavaScript

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.