Cross-frame variable reference in the & quot; with & quot; statement in js

Source: Internet
Author: User

Using the with statement in Script Programming can greatly reduce the length of code lines referenced by multi-level objects. This is a Syntax Sugar provided by JScript. Of course, if you do not use with, you can use temporary variables to achieve the same effect. However, naming variables does not mean a lot. Even in the MASM version, @ (and @ F and @ B) is provided to handle the problem of temporary labels, it seems that everyone is too lazy.

I also like to use with statements, which can save a lot of things and make the code clearer, especially when dealing with cross-frame variable references. This is not today because it is easy to hit the muzzle due to greed. When we go to the Cross-frame variable in with, we encounter an inexplicable bug for a long time.

The code I wrote is actually not complex. In a with statement, after many variable references and calculation operations in another frame, the result is assigned to a local variable of the function on this page. The sample code is roughly as follows ://...
Var hierarchies = [];
With (parent. frames ['xxxframework'])
{
// Reference the variables and values in xxxFrame to generate what I want
Hierarchies. push (xxx );
}

// Start using hierarchies
At this time, hierarchies is crazy, no matter how you get the value in it is always different from the value you want, but it is not completely wrong (I will explain why it is like this later, at the same time, this is also a place that seriously confused me about debugging, And it was miserable ).

Why? Finally, it turns out that there is also a variable named hierarchies in xxxFrame, which is also an array, and the content is similar to what I put, that's why I was confused. Besides this, because we have also written a similar with to retrieve data from different pages, and it works well, we subconsciously feel that, the hierarchies in with is the hierarchies outside. I do not remember whether the variable in xxxFrame was defined by myself. I was depressed for a while in debug :(.

In the JScript manual, with (objects) statements sets the default object for the statement. However, within the scope of with for variables, it has a search rule that gives priority to matching in the default object (objects. Although it is sometimes felt that the default object has a wider scope, it is like cross-frame. When the default object reference does not match, it matches the variable previously declared. It is undefined.

This article can only be honest with the "Waterloo in debug.

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.