繁体   English   中英

为什么调用 Function.apply.bind(fn, null) 调用的是 `fn.apply`,而不是 `Function.apply`?

[英]Why call to Function.apply.bind(fn, null) calls `fn.apply`, and not `Function.apply`?

假设我们有这样的代码:

let fn1 = Function.apply.bind(Math.max, null);
fn1([1, 10, 5]); // returns 10

我知道它是 ES6 中的一个spread function ,我们会这样写:

Math.max(...[1, 10, 5]);

但我无法理解Function.apply.bind如何Function.apply.bind它的魔力......

这就是我认为代码工作的方式,请纠正我的错误。

  1. 这个语句let fn1 = Function.apply.bind(Math.max, null); 创建一个新的功能Function.apply(null)this设定为Math.max并将其分配给fn1变量。
  2. 这个语句fn1([1, 10, 5]); 调用我们的新创建的功能以这种方式: Function.apply(null, [1, 10, 5])this将此功能设定为内部Math.max 但是这个调用是无效的……如果我们在 JS 控制台中复制/粘贴那一行 - 我们会得到Uncaught SyntaxError: Unexpected number

所以,我的问题是 - Function.apply如何知道它应该使用this等于Math.max并“旋转”/修改它对Math.max.apply调用?

更新

我才明白所有这些东西是如何工作的! 首先,让我们将代码重写为其“真实”形式:

let fn1 = this.Function.apply.bind(Math.max, null);
this.fn1([1, 10, 5]); // returns 10

魔法? this被隐式添加到“全局”方法/对象。 现在让我们重写这 2 段文字,向您展示魔法是如何工作的:)

  1. 这个语句let fn1 = Function.apply.bind(Math.max, null); 创建一个新函数newFunction.apply(null, ...args) ,其中newFunction == Math.max ,因此它与函数Math.max.apply(null, ...args)并将其分配给fn1变量.
  2. 这个语句fn1([1, 10, 5]); 调用newFunction.apply(null, [1, 10, 5])

魔法揭晓:)

重复更新

这个问题不是另一个问题的重复。 它不同,因为它更具体,在这里我特别问“为什么调用 Function.apply.bind(fn, null) 调用fn.apply ,而不是Function.apply ?” 而不是“apply.bind 是如何工作的?”。 这就像在问“为什么自行车齿轮使用踏板的动觉力而不是通过加速直接产生力”的问题? 与“自行车如何工作?”的问题相同。

让我们把它分成几部分,首先是bind()

bind()方法创建一个新函数,当调用this函数时,将其this关键字设置为提供的值,在调用新函数时,在任何提供的参数之前都有给定的参数序列。

因此,您bind Function全局对象中的apply方法bindMath.max

因此, bind()返回的函数fn1等效于this.apply() ,其中this实际上是Math.max ,正如您对bind()方法“询问”的那样。

这一行:

let fn1 = Function.apply.bind(Math.max, null);

……真的很麻烦。 :-)它要求bindFunction.apply ,创建将调用一个函数applyMath.maxthis值和null作为第一个参数(thisArg apply打电话时应该使用Math.max ; Math.max没有按”不关心this ,所以null很好)。

当您调用fn1 ,它会调用Function.apply并将this设置为Math.max ,并将null作为第一个参数,然后是您传递给fn1的数组。 apply使用null作为this调用Math.max ,并将数组中的条目作为离散参数。

暂无
暂无

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

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