我的代码是:
var length = 20;
function fn(){
console.log(this.length);
}
var o = {
length:10,
e:function (fn){
fn();
arguments[0]();
}
}
o.e(fn);
输出是20,1
,谁能告诉我为什么?
我的代码是:
var length = 20;
function fn(){
console.log(this.length);
}
var o = {
length:10,
e:function (fn){
fn();
arguments[0]();
}
}
o.e(fn);
输出是20,1
,谁能告诉我为什么?
当this
关键字出现在函数内部时,其值取决于函数的调用方式。
在您的情况下,fn()
在不提供 this 值的情况下调用,因此默认值为window
. 对于arguments[0]()
,上下文是arguments
对象,其长度为1
。
关键是在哪里调用函数并不重要,重要的是如何调用函数。
var length = 20;
function fn(){
console.log(this.length);
}
var o = {
length:10,
e:function (fn){
fn(); // this will be the window.
arguments[0](); // this will be arguments object.
}
}
o.e(fn);
此外,如果你想this
成为 object o
,你可以先使用call
or apply
,或bind
object 。
var length = 20;
function fn(){
console.log(this.length);
}
var o = {
length:10,
e:function (fn){
var fn2 = fn.bind(this);
fn.call(this); // this in fn will be the object o.
fn.apply(this); // this in fn will be the object o.
fn2(); // this also will be the object o.
}
}
o.e(fn);
让我们稍微扩展一下您的代码:
var length = 20;
function fn() {
console.log(this, this.length);
}
var o = {
length: 10,
e: function(fn) {
fn();
fn.call(this);
arguments[0]();
}
}
o.e(fn);
演示:http: //jsfiddle.net/ambiguous/Ckf2b/
现在我们可以看到调用时是什么this
(以及从何this.length
而来) 。fn
这给了我以下输出:
DOMWindow 0
Object 10
[function fn() { console.log(this, this.length); }] 1
我们也有三种调用函数的方式fn
:
fn()
: 就像任何旧函数一样调用它。fn.call(this)
:call
用于强制特定上下文(AKA this
)。arguments[0]()
:fn
通过arguments
对象调用。当您说. fn()
_ 全局恰好有一个属性:this
window
this
window
length
返回窗口中的帧数(frame 或 iframe 元素)。
这就是零(在我的输出中)的来源,您window.length
可能会有所不同。
我们在里面这么称呼e
is ,o.e(fn)
这就是意思(除了绑定函数和相关的复杂性)。所以in is and that与我们在控制台中得到的and相同(或多或少) 。注意到那个点再次出现了吗?this
e
o
o.e(...)
this
fn.call(this)
o
fn.call(this)
o.fn = fn; o.fn()
o
10
fn.call(o)
就好像o.fn = fn; o.fn()
第三个,arguments[0]()
,包含一个隐藏点,就像p = 'm'; o[p]
(或多或少)一样,o.m
就像arguments[0]()
一样fn = arguments[0]; arguments.fn = fn; arguments.fn()
。