JS预编译、AOGO

本文介绍了JavaScript的运行过程,包括检查语法错误、预编译和逐行解释执行。预编译分为函数上下文(AO)和全局上下文(GO),函数声明整体提升,变量声明提升但赋值不提升,还给出了预编译的具体步骤和执行过程。
  1. 运行过程
    检查通篇语法错误
    预编译:函数声明整体提升,变量只有声明提升,赋值不提升
    解释一行,执行一行

  2. 预编译的栗子

AO activation object 活跃对象,函数上下文

  • 寻找形参和变量声明
  • 实参赋值给形参
  • 找函数声明,赋值
  • 执行

GO global object 全局上下文

  • 找变量
  • 找函数声明
  • 执行
function test(a) {
    console.log(a); // function a() {}
    var a = 1;
    console.log(a); // 1
    function a() {};
    console.log(a); // 1
    var b = function() {};
    console.log(b); // function() {}
    function d() {};
}

test(2);

// AO = {
//     a: undefined -> 2 -> function a() {} -> 1
//     b: undefined -> function() {}
//     d: function d() {}
// }
function test(a, b) {
    console.log(a); // 1
    c = 0;
    var c;
    a = 5;
    b = 6;
    console.log(b); // 6
    function b(){}
    function d(){}
    console.log(b); // 6
}

test(1);

// AO = {
//     a: undefined -> 1 -> 5
//     b: undefined -> function b(){} -> 6
//     c: undefined -> 0
//     d: function d(){}
// }
console.log(a, b); // function a(){} undefined
function a(){}
var b = function() {}

// GO = {
//     b: undefined
//     a: function a(){}
// }
var b = 3;
console.log(a); // function a(a){}
function a(a) {
    console.log(a); // function a(){}
    var a = 2;
    console.log(a); // 2
    function a() {}
    var b = 5;
    console.log(b); // 5
}

a(1);

// GO = {
//     b: undefined -> 3,
//     a: function a(){}
// }

// AO = {
//     a: undefined -> 1 -> function a(){} -> 2
//     b: undefined -> 5
// }
a = 1;
function test() {
    console.log(a);
    a = 2;
    console.log(a);
    var a = 3;
    console.log(a);
}

test();
var a;
// GO = {
//     a: undefined -> 1,
//     test: function test(){}
// }

// AO = {
//     a: undefined -> 2 -> 3
// }
function test() {
    console.log(b); // undefined
    if (a) { // 此时a:undefined不会执行
        var b = 2;
    }

    c = 3;
    console.log(c); // 3
}

var a;
test();
a = 1;
console.log(a); // 1
function test() {
    return a;
    a = 1;
    function a() {}
    var a = 2;
}

console.log(test()); // function a() {}
function test() {
    a = 1;
    function a() {}
    var a = 2;
    return a;
}

console.log(test()); // 2
a = 1;
function test(e) {
    function e() {}
    arguments[0] = 2;
    console.log(e); // 2
    if (a) {
        var b = 3;
    }
    var c;
    a = 4;
    var a;
    console.log(b); // undefined
    f = 5;
    console.log(c); // undefined
    console.log(a); // 4
}

var a;
test(1);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值