我读到了MSDN上的JavaScript .call()方法,并看到了以下代码:
function callMe(arg1, arg2){
var s = "";
s += "this value: " + this;
s += "<br />";
for (i in callMe.arguments) {
s += "arguments: " + callMe.arguments[i];
s += "<br />";
}
return s;
}
document.write("Original function: <br/>");
document.write(callMe(1, 2));
document.write("<br/>");
document.write("Function called with call: <br/>");
document.write(callMe.call(3, 4, 5));
// Output:
// Original function:
// this value: [object Window]
// arguments: 1
// arguments: 2
// Function called with call:
// this value: 3
// arguments: 4
// arguments: 5从本文件中我了解到,.call()的目的是:
call方法用于代表另一个对象调用方法。它允许您将函数的此对象从原始上下文更改为thisObj指定的新对象。
有关的主要守则是:
document.write(callMe.call(3, 4, 5));为什么它返回3作为this值?那怎么会是全局对象?
发布于 2015-03-26 23:22:35
因为.call()的第一个参数是正在调用的函数中的this值。在您的例子中,这是3。
https://stackoverflow.com/questions/29290640
复制相似问题