【发布时间】:2013-02-20 03:51:47
【问题描述】:
我有一个数组迭代器函数:
function applyCall(arr, fn) {
fn.call(arr[0], 0, arr[0]);
}
还有一些代码
var arr1 = ['blah'];
applyCall(arr1, function (i, val) {
alert(typeof this); // object WHY??
alert(typeof val); // string
alert(typeof(this === val)) // alerts false, expecting true
});
为什么typeof this 在内联函数object 中,而不是string?
jsfiddle here
【问题讨论】:
-
只是一个注释。我相信最后一条语句应该是
typeof this === typeof val而不是typeof(this === val) -
因为
this不再指代arr1。一旦你在函数中使用this,它就会引用function,它是一个对象。 -
@icanc - 这不是真的。
.call()的第一个参数决定了函数内部的this是什么。
标签: javascript this typeof