skip to content
寻找莉莉丝

函数调用的预编译

/ 3 min read / 次阅读

一、前言

上一篇我们说到了脚本的预编译,这一节讲一讲函数调用的预编译。


二、函数调用的预编译

1. 过程

1.创建活动对象AO(Active Object)

2.预编译:

  • 产生作用域链(scope chain)

  • 初始化arguments

  • 初始化形参,将arguments中的值赋值给形参

  • 找出所有的变量声明,按照变量名加入AO,如果已存在,忽略。

  • 找出所有的函数声明,按照函数名加入AO,如果已经存在同名变量或者函数,替换。

  • this初始化

    3.解释执行代码

2. 解析

1.函数中的所有变量声明,在函数的预编译阶段完成,所有变量与实际书写位置无关。

function f() {
	console.log(aa); // undefined
	var aa = 5;
	console.log(aa); // 5
}
f();

2.函数中的所有函数声明,在函数的预编译阶段完成,所有变量的声明与实际书写位置无关。

function f() {
	console.log(haha);
	function haha() {
		console.log(123);
	}
}
f();

3.函数中,如果变量与函数同名,那么函数将覆盖变量。

function f() {
	console.log(haha);
	var haha = 123;
	function haha() {
		console.log(456);
	}
}

4.函数中,只有函数能覆盖变量,变量无法覆盖函数。

function f() {
	console.log(haha);
	function haha() {
		console.log(123);
	}
	var haha = 456;
}
f();

5.函数中,后面的函数声明会覆盖前面的函数声明,并且忽略参数。

function f() {
	console.log(haha);
	function haha(a) {
		console.log("aaa");
	}
	function haha(a, b) {
		console.log("bbb");
	}
}
f();

6.当函数预编译后,遇到需要访问的变量或函数,优先考虑自己AO中定义的变量和函数,如果找不到,才会在其定义的上层AO中寻找,直到到达GO。

var scope = "global";
function t() {
	console.log(scope); // undefined
	var scope = "local";
	console.log(scope); // local
}
t();
console.log(scope); // global

三、本节思维导图