预编译

预编译的四部(在其中执行过的运行时不执行)

  • 创建AO对象
  • 找形参合和变量声明,将变量和形参名作为AO对象,值为undefined
  • 将形参和实参统一
  • 在函数体里面找函数声明,值赋予函数体(将函数名作为AO对象的属性名写进AO对象)
function fn(a){
    console.log(a);//答案1
    var  a = 123;
    console.log(a);//答案2
    function a(){};//函数声明
    console.log(a);//答案3
    var b =function (){};//函数表达式
    console.log(b);//答案4
    function d(){}//函数声明
}
fn(1);

  1. 第一步

    AO{}

  2. 第二部
    AO{
    a:undefined //变量声明+形参
    b:undefined //变量声明
    }

  3. 第三部
    AO{
    a:1*//实参传值*
    b:undefined;
    }

  4. 第四步
    AO{
    a:function a(){};
    b:function b(){};
    d:function d(){}
    }
    所以答案:
    答案1:function a(){}
    答案2:123
    答案3:123
    答案4:function (){}

例二(有全局变量)

function test(){
console.log(b);//答案1
if(a){
var b = 100;
}
console.log(b);//答案2
c = 234;
console.log(c);//答案3
}
var a;
test();
a=10;
console.log(c);//答案4

1.GO{
a:undefined;
}
AO{
b:undefined;
}
2.运行
GO{
a:undefined;
c:234;
}
AO{
b:undefined;
}
所以答案:
答案1:undefined
答案2:undefined
答案3:234
答案4:234

例三

    function bar()
{
    return foo;
    foo = 11;
    function foo(){}
    var off = 11;
}    
console.log( bar()) ;

答案:function foo(){};

例四

a = 100;
function demo(e){
function e(){}
arguments[0]= 2;
console.log(e);//2
if(a){
var b = 123;
function c(){}
}
var c;
a = 10;
var a;
console.log(b);//undefined
f = 123;
console.log(c);//undefined
console.log(a);10
}
var a;
demo(1);
console.log(a);100
console.log(f);123

在这里插入图片描述

例五

var x = 1,y = z = 0;
function add(n){
        return n = n+1;
}
y = add(x);
function add(n){
        return n = n+3;
        }
      z=  add(x);

求x,y,z的值:1,4,4

AO{
      add:function(){ 
      return n=n+3;
      }
}

从上向下执行,会被最后的一个所覆盖,然后再调用执行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值