预编译

 

脚本的预编译
1.//没有 var 的变量都不是变量声明,全部认为window的全局变量,不参与预编译
console.log(aa);//undefind
var aa = 0;
console.log(aa);//0
 
console.log(bb);
bb = 5;
console.log(bb);

 
2.//脚本中命名相同的函数会覆盖变量,只有函数覆盖变量,变量不能覆盖函数。
console.log(xx);
function xx(){
console.log("qq");
}

var xx = 1;

 

3.脚本中相同命的函数,后面函数 会覆盖前面函数

ff(1)

function ff(a){
console.log("ff1");
 
}
function ff(a,b){
console.log("ff2");
 
}

 

函数的预编译

 基本同上

例1
var scope = 'globe';
function f(){
console.log(scope);//undefind
var scope = 'locl';
console.log(scope);//locl
}
f() console.log(scope);//globe

预编译过程
1.找出变量声明
scope:undefind;
f:function;
f-AO:
scope:undefind;
预编译结束
输出
f-AO输出
undefinde
locl
GO输出:
globe

例2

var scope = 'global';
function t(){
    var scope = 't-local';
    function t2(){
        console.log(scope); 
        var scope = 't2-local';
        console.log(scope);  
    }
    t2()
    console.log(scope) 
}
t()
console.log(scope);

预编译过程
先拿到变量
GO:
scope:undefinde
t:function
t-AO:
scope:nudefind
t2:function
t2-AO:
scope:undefinde;
输出:
t2-AO输出:
undefined;
t2-local;
t-AO:
t-local;
GO:
global

例三

function test(x,x){
    console.log(x);//function
    x= 5;
    console.log(arguments);//[12,5]
    function x(){}
}
test(12,13)
预编译过程
先拿到变量
test:function
test-AO:
arguments:[12,13];//绑定形参,后面的覆盖前面的;
x-arguments[1] = 13;
x:function;
x=5;
x-arguments[1]=5;
输出
function
[12,5]

例4

var b = 'cba';
function a(a,a){
    console.log(a);
    console.log(b);
    var b = 'abc';
    a()
    function a(){
        console.log(a);
        console.log(b);
        
    }
}
a(5,10)
预编译过程
先拿到变量
GO:
b:undefined;
a:function;
a-AO:
arguments:[5,10];
a-arguments:[10];
b:undefined;
a:function;
输出
a-AO:
function
undefined
a.a-AO:
function
abc

 

 

posted @ 2019-08-07 16:38  假亦真LL  阅读(249)  评论(0编辑  收藏  举报