programmer
programmer copied to clipboard
ECMAScript规范
ECMAScript规范
11.2.3 函数调用:Function Calls
这里讲了浏览器在进行函数调用时的执行过程:
-
计算MemberExpression的结果赋值给
ref - 令
func为GetValue(ref) - 令
argList为解释执行Arguments的结果 , 产生参数值们的内部列表 (参考11.2.4). - 如果
Type(func) is not Object,抛出一个TypeError异常 . - 如果
IsCallable(func) is false,抛出一个TypeError异常 . -
如果
Type(ref)为Reference,那么- 如果
IsPropertyReference(ref)为true, 那么 让this的值为GetBase(ref). - 否则, 当
ref的基值base为Environment Record时,那么让this的值为ImplicitThisValue(ref).
- 如果
-
否则,
Type(ref)的值不为Reference时,那么this的值为undefined - 返回调用
func的[call]内置方法的结果 , 传入thisValue作为this值和列表argList作为参数列表
Reference
The Reference type is used to explain the behaviour of such operators as delete, typeof, and the assignment operators. Reference 类型就是用来解释诸如 delete、typeof 以及赋值等操作行为的。 A Reference is a resolved name binding. 被解析的命名绑定
Reference 由三个组成部分,分别是:
- base value:为 property 对象或环境记录(environment record)。
- referenced name:为标识符或属性名
- strict reference:是否开启了严格模式
"use strict";
var foo;
// 对应的Reference是:
var fooReference = {
base: EnvironmentRecord,
name: 'foo',
strict: true
};
// or
foo.bar;
// 对应的Reference是:
var barReference = {
base: foo,
name: 'bar',
strict: true
};
// or 使用未声明的变量
a;
varaReference = {
base: undefined,
name: 'a',
strict: true
};
另外需要记住几个方法:
- 使用
GetBase获得Reference的base value - 使用
IsPropertyReference判断base value是否为一个对象,是就返回true - 在
8.7.1中,讲了一个用于从Reference类型获取对应值的方法:GetValue。
var foo = 1;
var fooReference = {
base: EnvironmentRecord,
name: 'foo',
strict: false
};
GetValue(fooReference) // 1;
- 在
10.2.1.1.6中ImplicitThisValue()始终返回undefined
实例解释
var value = 1;
var foo = {
value: 2,
bar: function () {
return this.value;
}
}
//示例1
console.log(foo.bar());
//示例2
console.log((foo.bar)());
//示例3
console.log((foo.bar = foo.bar)());
//示例4
console.log((false || foo.bar)());
//示例5
console.log((foo.bar, foo.bar)());