Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript.call()函数;这";返回值3_Javascript - Fatal编程技术网

JavaScript.call()函数;这";返回值3

JavaScript.call()函数;这";返回值3,javascript,Javascript,我正在阅读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 /&

我正在阅读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

为什么将
3
返回为
this
值?这是全局对象吗?

因为
.call()
的第一个参数是您正在调用的函数中的
this
的值。在您的例子中,即
3

在使用call或apply时,第一个参数是要应用的函数或call'd中“this”的值。如何传递它3个参数,它只接受2个。这是一些非常奇怪的代码-我从未见过
参数用封闭函数的名称限定。尝试使用Mozilla开发人员网络(MDN)而不是MSDN.Oh,它还设法滥用
来。。。在
中读取伪数组@Mathemats不正确,JS不会限制您提供的参数数量,无论您在函数括号之间放置什么。您可以使用多于或少于定义为接受的参数来调用它。此外,此
.call()
调用仅传入两个参数:
4
5
(将
3
用作
值)。谢谢。即使这是正确的,也没有什么问题,因为为什么传递名为“3”的对象inIt不是名为
3
的对象(在JS中变量不能以数字开头),它实际上是数字3。
document.write(callMe.call(3, 4, 5));