The JS function called Parent.func () in the IFRAME is run in the scope they are defined in, not the scope in which they are executed

Source: Internet
Author: User

There is a document that defines a function func (), while embedding an IFRAME in the document, the method of calling the parent window in this iframe: Parent.func (), originally I thought this function's environment is in this IFRAME itself inside , the test found that although this function was called in the IFRAME, the operating environment is still in the parent window.

So also verified in the JS authoritative guide:JS functions run in their defined scope, not the scope of their execution.

Another example

var a=1; function F1 () {console.log (a);}; function F2 (i) {var a=2; F1 ();} F2 (); // ----1

When the code runs to the call of a function, the scope switches to the scope of the function itself, when the function call is complete and then the original scope is switched, the two scope chains are independent, and the scope chain of a function is determined by where it is defined. It is also because functions run in the scope they are defined to cause closures to be available.

Essential Explanations:

ECMA-262 Standard:

When a function is defined, the scope chain that defines the moment is linked to the [scope] property of the function object.

When a function object is called, an active object (that is, an object) is created, and then, for each function's formal and local variables, it is named as the active object's named property, then the active object is the most front-end of the scope chain (scope chain) at this time, and the function object's [ [Scope]] Added to scope chain.

For example, when F1 and F2 are defined in the example above, there is only one level scope for the Window object in [[scope]],

When F2 () is called, an active object is created that includes the I and a two properties, at which point the F2 [[scope]] includes the active object and the previous Window object two-level scope;

The active object created when the call to F1 () is empty.

So when running F2 (), the variable that can be accessed is: {i:undefined,a:2}---->window Object {a:1,f1:...,f2: ...},

The variable that runs F1 () in F2 () is: Window object {a:1,f1:...,f2: ...},

So the A that is accessed at this point belongs to the Window object and cannot be accessed to the local variable A in the F2 scope chain.

The JS function called Parent.func () in the IFRAME is run in the scope they are defined in, not the scope in which they are executed

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.