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

382

被折叠的 条评论
为什么被折叠?



