简体   繁体   English

Javascript调用和应用方法

[英]Javascript call and apply methods

I am confused by this code: 我对此代码感到困惑:

Function.prototype.apply.call(Math.floor, undefined, [1.75]); // 1

I read this post javascript apply and call methods and chain together and I understand the meaning of chaining apply and call methods together. 我读了这篇文章javascript apply并调用方法和链接在一起 ,我理解链接应用和调用方法的含义。

However, I am still confused about the syntax. 但是,我仍然对语法感到困惑。 The correct syntax for call is function.call(thisArg, arg1, arg2, ...) . 调用的正确语法是function.call(thisArg, arg1, arg2, ...) But in this case, why could the call method take these three parameters ( target , thisArgument , argumentsList ), which is the same as Reflect.apply(target, thisArgument, argumentsList) ? 但在这种情况下,为什么调用方法可以采用这三个参数( targetthisArgumentargumentsList ),这与Reflect.apply(target, thisArgument, argumentsList)

Actually, call method can take all comma separated arguments and pass it to the called method. 实际上,call方法可以采用所有逗号分隔的参数并将其传递给被调用的方法。 While as you see here (target, thisArgument, argumentsList) Second two arguments work as arguments for apply method and we know that apply method gonna need an array of argument, we have sent third parameter as an array. 正如你在这里看到的那样(target, thisArgument, argumentsList)两个参数作为apply方法的参数,我们知道apply方法需要一个参数数组,我们已经将第三个参数作为数组发送。

So here the execution can be simplified to : 所以这里的执行可以简化为:

Math.floor.apply(undefined, [1.75]);

Or simply Math.floor(1.75) // Obv with undefined as reference of this 或者只是Math.floor(1.75) // Obv with undefined as reference of this

    Function.prototype.apply.call(Math.max, undefined, [1,75]); // 1

code above change thisArgu in Function.prototype.apply to Math.max 代码在以上Function.prototype.apply的变化thisArguMath.max

let's talk about change thisArgu in Function.prototype.apply to Math.max 让我们谈谈将Function.prototype.apply中的thisArgu更改为Math.max

noramlly, this in Function.prototype.apply should be Function.prototype noramlly, Function.prototype.apply中应该是Function.prototype

so change thisArgu in Function.prototype.apply to Math.max means: 所以将Function.prototype.apply中的thisArgu更改为Math.max意味着:

change Function.prototype to Math.max if met 'this' when executing in Function.prototype.apply() 如果在Function.prototype.apply()中执行'met',则将Function.prototype更改为Math.max

so code above can be understood like this when execute and meet 'this' : 因此,在执行并遇到'this'时,可以像这样理解上面的代码:

    Math.max.apply(undefined,[1,75])

now you understand? 你现在明白了?

if we go on ,you will find 如果我们继续下去,你会发现

    Math.max.apply(undefined,[1,75])

can be understood like: 可以理解为:

    Window.max(1,75)//error;

so , i guess 'this' word doesn't exist in Math.max function content,so it gets no error; 所以,我猜这个'字'在Math.max函数内容中不存在,所以它没有错误;

The syntax for call() is: call()的语法是:

someFunctionOrMethod.call(...)

So, this code is calling the apply method of the Function type. 因此,此代码调用Function类型的apply方法。

// Call the "apply()" method
Function.prototype.apply.call()

// With the following arguments passed to "apply"
(Math.floor, undefined, [1.75])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM