Understandable JavaScript scope (ii)

Source: Internet
Author: User

Then the previous article, on the basis of the above to do a deformation.

Deformation one

Let's take a look at the demo below


<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><script>            var a = 1;        function fn1 () {            alert (a);            A = 2;        }        function Fn1 (a) {            alert (a);            A = 2;        }        FN1 ();        alert (a);        var a = 1;        function Fn1 (a) {            alert (a);            A = 2;        }        FN1 ();        alert (a);               </script></span>


Code Analysis

Look at the above code, there is another kind of wood dizzy feeling, this is where the sacred, can not understand ha! It's okay, let's analyze it slowly.

or according to the analysis I mentioned in the previous blog several steps, to analyze.

1. Get some stuff.

In the browser to resolve the pre-parsing, is to find some Var, function of things, and then after the PK placed into a warehouse inside. And the PK level is var--function function (first come)--function (later). After the PK, the warehouse is placed in the following things:

2. Line-wise interpretation of code

After the pre-parsing JS, starting from top to bottom execution code, when executed to fn1 () code, strange things appear, because Fn1 () is called function block, so began the above two processes, parsing-interpretation, Well, after reading it, look at what's changed in the warehouse.

Why is there another one? In this analysis, did not find the emergence of Var ah, a front and no ha, why will more than a? We should not forget that when parsing fn1 (a), where a is the equivalent of FN1 (Var a), so the problem is here, there is more than a var effect, so the above results will occur.


3. Execute the parsed code here

So when the alert (a) is executed, because it was assigned a value of undefined at the time of parsing, the undefined window pops up.

The results after execution are as follows


Take down the execution, alert (a), this time you know the demerit! The pop-up is definitely 1 ha.

Not to be continued




Understandable JavaScript scope (ii)

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.