我有一个数组迭代器功能:的JavaScript:意外的typeof结果
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这个=== typeof val'而不是'typeof(this === val)' – techfoobar 2013-02-20 03:54:21
因为'this'不再指'arr1'。一旦你在函数中使用'this',它就会反驳那个作为对象的'function'。 – 2013-02-20 03:55:07
@icanc - 事实并非如此。 '.call()'的第一个参数决定了'this'在函数内部。 – techfoobar 2013-02-20 03:56:09