Pre-compiled JavaScript to enhance and variable

Last year, eating a long, long time rhinoceros (JS authoritative guide), eating too dental pain, to revisit today's big rhinoceros found quite simple, until that elusive variable lift, now a will, so he wrote this blog, when you revisit a little longer.
JavaScript is an interpreted language, want to run JavaScript code requires two stages of
compilation stages: stage compiling JavaScript is what we often say that the pre-interpretation (pretreatment) stage, at this stage will be completed by the JavaScript interpreter JavaScript script code conversion to bytecode
execution stage: at compile bytecode JavaScript interpreter generates machine code, from top to bottom and are executed in order by execution environment
stack memory: JS code used to provide an environment for execution, i.e. scope ( global scope / private scope)
heap memory: to store reference values of the data type. Is stored in the object attribute names and values, the function stores the code string.

What is pre-compiled?

Pre-interpretation: JavaScript code before execution, the default browser will first of all with the var and function are defined in advance of the declaration or
statement 1, the variable advance

  console.log(a);//undefined
  var a = 1;

Compiled into

  var a;
  console.log(a);
  a = 1;

2, function declarations in advance
var => at the time of the pre-interpretation of the statement just ahead of the
function => at the time of the pre-interpretation of a statement in advance of + definitions are completed
for the cycle, is function scope, if, switch not.
3, just ahead of window variable scope, the function of which, only when calls, and then declare in advance.

Actual articles

The first bomb:

var a=1,b=2,c=3;
function main(a){
    a = 4;
    var b = 5;
    c = 6;
}
main(6);
console.log(a);console.log(b);console.log(c);//1,2,6
a输出为6,是因为a是作为参数传进去,传到函数体,那么便是私有变量。
b输出为2,是因为在函数里var b,重新定义,由于在函数体内,所以也是私有变量。
c输出为6,是因为在函数里,他会去查找c,如果没有,往上一级一直查找,所以,会更改全局变量,输出为6

The second bomb:

var num = 0;
function main(num1,num2){
    console.log(num);//undefined
    var num = num1+ num2;
    console.log(num);//30
}
main(10,20);
console.log(num);//0
第一个为undefined是因为变量提升,var num;所以为undefined
第二个输出30,是因为参数进去了,所以为30
第三个为0,是因为在方法里,用了var,重新定义了一个局部变量,只是名字和全局变量一样而已,所以输出的时候,还是全局变量,所以为0

The third bomb:

            if(!"num" in window){
                var num = 12;
            }
            console.log(num);//undefined
            原因在于变量提升,if不是函数作用域,所以var num会提升到最上面,所以也就成了全局作用域了。

Functions are first-class citizens:

Fourth bomb:

            function a(){}
            var a;
            console.log(typeof a);
            a();
            输出function,原因是因为函数会首先被提升

Fifth bomb:

            fn()
            var fn = function a(){
                console.log("ok");
            }
            报错,fn is not a function,是因为函数提升,var fn = xxx ; "="后面的不会提升,只会提升前面部分。

The sixth bomb:

            var a = 1;
            function a(a){
                console.log(a);
                var a = 3;
            }
            a(2);
            报错,a is not a function。是因为函数声明,比变量声明优先级更高,并且把变量覆盖,但是变量可以重新定义,var a = 3;又把函数覆盖了。所以会输出不是一个函数。

For ease of understanding, the function will be compiled as follows:

            function a(a){
                console.log(a);
                var a = 3;
            }
            a = 1;
            a(2)

The seventh bomb:

function a(){return true};
console.log(a);
console.log(a());
第一个会输出整个函数字符串,第二个则会调用整个函数。这就是区别啦~
function a(){}
var a = function(){}
两种写法都是一样的,只是一个是函数方式,一个是函数字面量方式。当然,这只在弱类型语言行得通~

Above, all knowledge is pre-compiled and variable lift of friends ~

Reproduced in: https: //www.jianshu.com/p/8c9a2506f939

Guess you like

Origin blog.csdn.net/weixin_34146986/article/details/91160017