Often misunderstood a JS small problem

Source: Internet
Author: User

Today inadvertently saw a JS pen test questions, can not help but think of beginners JS that will be a variety of topics of bitterness, although now also will be a pen test abuse, but after all better than before, at least for this problem I can still correctly answer up:

var x = 1; function Printx () {    console.log (x);    } function Show (f) {    var x = 2;    (function() {        f ();     //

The results will print 1 instead of 2 in the background. This is somewhat counterintuitive, because many people will mistakenly think: the function show F () in the execution, because there is no X in this scope, so the upper scope of the search for X, when found in the scope of the upper function show Var x=2, then the X is determined to 2, or will continue to look up, Until window ... Finally used the so-called "Scope chain"! Exultation, in fact, think you are wrong!

First: There is a noun called "free variable", the free variable is : if the scope (function) A is used in the variable x, and x is not defined in scope (function) A, then for scope (function) A, x is a free variable.

Second: Understand the sentence: JS does not have block-level scope, the only block-level scope is implemented with functions, that is, only the function can create a separate scope. If, for code blocks are not! So one of the scopes in this article can be understood as a function.

For nested functions According to the above: to find free variables is to look up a layer along the scope chain, so the understanding is right. But this understanding is too one-sided, can even produce errors, such as the above problem. The correct understanding should be that the search for a free variable is found in the scope where the function (scope) was created. The scope of creating this function is not necessarily the parent scope of its location, (not in the code structure, that is, the child-parent relationship) above this is the case: function show in the PRINTX function as a parameter in the execution, the execution will look for the scope to create it, It is obvious that the scope for creating the PRINTX function is not show, but the global window (because the PRINTX function was created in the window), so it looks for the X in the global window, this X is 1. So it prints 1, which is the positive solution.

I don't know what I'm explaining. In short: When a function uses a free variable, it is looked for in the scope where the function was created. "Look for in the parent scope" may be biased.

Often misunderstood a JS small problem

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.