The use of Javascript:with and extended scope chains

Source: Internet
Author: User

Transferred from: http://lllt.iteye.com/blog/1246424

The 66th page of JavaScript Advanced Programming (Second Edition) mentions: "Because the variable object of the WITH statement is read-only, the resulting URL becomes part of the function execution environment and can therefore be returned as a function value." "Don't know who read it and feel confused?"

First take a look at the example he cited:

JS Code
function BuildUrl () {       var qs= "? Debug=true";       With (location) {            var Url=href+qs;       }       return URL;  }  var result=buildurl ();    alert (result);  

  

If you haven't read the book and need to learn JavaScript, think and try running the example.

The last popup is not undefined, but the value of your static page address +qs.

Take a look at the function of the WITH statement:

Popularly speaking, it is the reference object, and the property on the object operation, its role is to omit the repeated writing of the object name, play a role in simplifying writing.

But there are a few questions to note:

1. With code blocks, the JavaScript engine handles the variable by first looking for a property that is not the object, or , if it is, stopping. If the lookup is not continued, it is not a local variable. (The idea mentioned in the JavaScript Advanced Programming (second Edition) is the opposite, but the example proves that it is wrong and will be introduced next)

2, even if the variable is redefined with the Var operator in the WITH statement (the variable is a property of the With reference object), the property of the object is also assigned a value if it is a writable property.

3. If you want to add multiple attributes to a reference object and assign a value to each property through the With statement, this is not possible! That is, the only property that is to be assigned is an object that already exists and can be written to (it cannot be a read-only property).

Let's take a look at the words from the beginning.

"Because the variable object of the WITH statement is read-only, the resulting URL becomes part of the function execution environment and can therefore be returned as a function value." ”

Conversely: If the variable object of the WITH statement is writable ... Just now the 3rd mentioned that the object can not be written to the original non-existent properties, the first way to understand, there is another meaning.

What about the extended scope chain?

Generally, "because the variable object in the scope of the WITH statement block is read-only, the identifier defined in his layer cannot be stored to this layer, but to its previous scope". It is also understood that there is a "read-only" meaning.

In the scope of JavaScript (scope, think of the function block, each function will have a function name, even if the anonymous function has an empty functional name), then create scope, the identifier of this layer can be pinned to this scope, and with the scope of the statement block ' The variable object ' is read-only, cannot store identifiers, and can only be stored on its upper layer, which is the extended scope chain. In fact, this and the above said can not add attributes to the object has the same place.

In fact, it is perfectly understandable that in JavaScript there is no block-level scope, except that the other block-level code does not have its own scope in addition to the function.

Now let's talk about the problem of how variables are handled in the with code block mentioned earlier

To speak with facts:

JS Code
var o={href: "Sssss"};   var href= "1111";   function BuildUrl () {       var qs= "? debug=true";              with (o) {            href= "2222";             var url=href+qs;       }            return URL;  }   var result=BuildUrl ();  alert (result);  alert (href);  

Results: 2222?debug=true + 1111

Obviously, the WITH statement did not change the value of the variable href, but instead changed the href property of the O object.

That is, with the first look for the properties of the related object, if not, change the value of the variable. You can remove the href attribute from the above example O object to see.

The use of Javascript:with and extended scope chains

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.