看下面这段代码会在控制台上输出什么内容?
1 <script> 2 var url="fang.com"; 3 var obj={ 4 url:"soufun.com", 5 func:function(){ 6 return this.url; 7 } 8 }; 9 10 console.log((obj.func)()); 11 console.log((1&&obj.func)()) 12 </script>
答案是
1 soufun.com 2 fang.com
今天一同事拿着这段代码,问我为何第二次输出的是"fang.com"。
这段代码只能在非严格模式下执行,那么我们就看看有关this的规范的介绍,Standard ECMA-262 3rd (ECMA-262标准 第三版)
10.1.7 This There is a this value associated with every active execution context. The this value depends on the caller and the type of code being executed and is determined when control enters the execution context. The this value associated with an execution context is immutable.
对于每个活动执行上下文,都有一个 this 值与其相关联。在控制进入执行上下文时,根据调用者和被执行代码的类型决定这个值。与执行上下文相关联的 this 值是不可改变的。
10.2 Entering An Execution Context Every function and constructor call enters a new execution context, even if a function is calling itself recursively. Every return exits an execution context. A thrown exception, if not caught, may also exit one or more execution contexts. When control enters an execution context, the scope chain is created and initialised, variable instantiation is performed, and the this value is determined. The initialisation of the scope chain, variable instantiation, and the determination of the this value depend on the type of code being entered.
10.2 进入执行上下文
每个函数和构造器调用都要进入一个新的执行上下文,即使是函数在递归地调用自身。每次返回都会退出执行上下文。未被 catch 的异常抛出有可能退出一个或多个执行上下文。
当控制进入执行上下文时,创建并初始化作用域链,进行变量初始化,并决定 this 值。
作用域链的初始化,变量的初始化和 this 值的决定取决于进入的代码类型。
10.2.1 Global Code The scope chain is created and initialised to contain the global object and no others. Variable instantiation is performed using the global object as the variable object and using property attributes { DontDelete }. The this value is the global object.
10.2.1 全局代码
• 被创建并初始化的作用域链只包含全局代码。
• 进行变量初始化时,把全局对象作为可变对象,属性特征为 { DontDelete }。
• this 值为全局对象。
10.2.2 Eval Code When control enters an execution context for eval code, the previous active execution context, referred to as the calling context, is used to determine the scope chain, the variable object, and the this value. If there is no calling context, then initialising the scope chain, variable instantiation, and determination of the this value are performed just as for global code. The scope chain is initialised to contain the same objects, in the same order, as the calling context‘s scope chain. This includes objects added to the calling context‘s scope chain by with statements and catch clauses. Variable instantiation is performed using the calling context‘s variable object and using empty property attributes. The this value is the same as the this value of the calling context.
10.2.2 求值代码
当控制进入求值代码的执行上下文时,把前一个活动的执行上下文引用为调用上下文,用它决定作用域链、可变对象和 this 值。若调用上下文不存在,就把它当作全局对象,进行作用域链和变量的初始化及 this 值的决定。
• 被创建并初始化的作用域链与调用上下文包含相同的对象,顺序也一样;使用 with 声明或 catch 语句给调用上下文的作用域链添加的对象也包括在内。
• 进行变量初始化时,使用调用上下文的可变对象,属性特征为空。
• this 值与调用上下文的 this 值相同。
10.2.3 Function Code The scope chain is initialised to contain the activation object followed by the objects in the scope chain stored in the [[Scope]] property of the Function object. Variable instantiation is performed using the activation object as the variable object and using property attributes { DontDelete }. The caller provides the this value. If the this value provided by the caller is not an object (note that null is not an object), then the this value is the global object.
10.2.3 函数代码
• 被创建并初始化的作用域链包含一个活动对象,该对象之后是函数对象的[[Scope]]属性存储的作用域链中的对象。
• 进行变量初始化时,把该活动对象作为可变对象,属性特征为 { DontDelete }。
• this 值由调用者提供。若调用者提供的 this 值不是一个对象(注意,null 不是对象),则 this 值为全局对象。
一开始的代码中,代码类型是函数代码,其中this值由调用者提供。
运算符的优先级,因.(属性存取) [](数组下标) ()(函数调用) 都属于同级(最高级)并且是左结合。
对于代码(obj.func)()和obj.func()是一样的。都是先获取obj的方法,再执行。他的调用者是obj。
对于代码(1&&obj.func)(),1&&obj.func是个算式,这个算式返回obj.func的值。那么就相当于
(function(){return this.url;})()
其调用者是window,所以(function(){return this.url;})()的返回值是"fang.com"
参考
http://bclary.com/2004/11/07/
http://zhangbo-peipei-163-com.iteye.com/blog/1773959
创建于2014.10.21,完成时间时间:2014.10.23