为什么 apply 尚未绑定到 Javascript 中的函数? [英] Why is apply not already bound to functions in Javascript?

查看:24
本文介绍了为什么 apply 尚未绑定到 Javascript 中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设,为了这个问题,我希望能够在 Javascript 中创建一个函数,将一个数组的所有元素附加到另一个数组.如果您有权访问目标数组,则实现此目的的一种方法是说:

Assume, for the sake of this question, that I want to be able to create a function in Javascript that appends all of the elements of one array to another array. One way to achieve this, if you have access to the destination array, is to say:

var destination = [1,2,3];
var source = [4,5];
Array.prototype.push.apply(destination, source);
console.log(destination); // [1,2,3,4,5]

现在,由于 Array.prototype.push.apply 非常丑陋,我想将其别名为更好的东西,例如:

Now, since Array.prototype.push.apply is pretty ugly, I want to alias it to something nicer, like:

var pushAll = Array.prototype.push.apply;

我应该能够使用两个参数调用,上下文(目标)和参数数组(源).但是,当我尝试使用别名时,会发生这种情况:

Which I should be able to call with two arguments, the context (destination) and an array of arguments (source). However, when I try to use the alias, this happens:

pushAll(destination, [6,7]);
TypeError: Function.prototype.apply was called on [object global], which
is a object and not a function

很明显 apply 函数没有绑定到 push,这让我尝试了这个:

So clearly the apply function is not bound to push, which led me to try this:

var pushAll = Function.prototype.apply.bind(Array.prototype.push);
pushAll(destination, [6,7]);
console.log(destination); // [1,2,3,4,5,6,7,8]

这显然工作正常.我的问题是,为什么我必须绑定push方法才能申请?不应该 Array.prototype.push.apply 已经绑定应用了吗?为什么以不同的名称调用它会导致在未绑定的上下文中调用它?

Which clearly works fine. My question is, why do I have to bind the push method to apply? Shouldn't Array.prototype.push.apply already be bound to apply? Why does calling it under a different name result in calling it on an unbound context?

推荐答案

为什么一定要绑定push方法才能申请?

why do I have to bind the push method to apply?

相反:您必须将 apply 方法绑定到 Array push 函数 - 您也可以将其绑定到其他函数!否则apply 不知道哪个 方法与参数一起应用.

It's the other way round: You have to bind the apply method to the Array push function - you can bind it to other functions as well! Otherwise apply doesn't know which method to apply with the arguments.

Function.prototype.apply.bind(Array.prototype.push); 确实调用了 bind 函数 on apply 函数以 push 作为参数,然后绑定 apply 的参数 on.结果函数 pushAll 将在调用时调用 apply on push 函数,并传递它的参数(数组和参数数组)到它.

Function.prototype.apply.bind(Array.prototype.push); does call the bind function on the apply function with push as the argument, the argument on which apply is then bound. The resulting function pushAll will, when called, invoke apply on the push function, and pass it's argument (the array and the arguments array) to it.

Array.prototype.push.apply 不是应该已经绑定应用了吗?

Shouldn't Array.prototype.push.apply already be bound to apply?

没有.JavaScript 旨在在函数调用时绑定上下文,而不是在它被称为属性时绑定 - 属性访问没有隐式绑定.否则 Array.prototype.push 将已经绑定到 Array.prototype,在你可以调用任何函数方法之前,比如 bind/apply 并尝试使用不同的上下文.

Nope. JavaScript is designed to bind the context at the call of a function, not already when it's being referred to as a property - there is no implicit binding on property access. Otherwise Array.prototype.push would already be bound to Array.prototype, before you could call any Function methods like bind/apply on it and try to use it with a different context.

为什么以不同的名称调用它会导致在未绑定的上下文中调用它?

Why does calling it under a different name result in calling it on an unbound context?

不是名字不同,而是风格不同.(未绑定的)函数确实将它们的 this 值设置为对象,当它们作为方法被调用时,即当对被调用函数的引用是属性访问时:destination.push().

It's not so much different name, but different style. (Unbound) Functions do get their this value set to the object when they are called as a method on it, i.e. when the reference to the called function is a property access: destination.push().

这提供了很大的灵活性,您可以从一个对象借用"函数并在其他对象上调用它们,仍然是相同的(未绑定的)函数.这在函数对象不是一等对象的语言中是不可能的.

This allows for great flexibility, you can "borrow" functions from an object and call them on other objects, still being the same (unbound) function. This is rather impossible in languages where function objects are no first-class objects.

如果函数(即使它们本来是方法)被称为普通函数(pushAll()),它们的this值将是undefined(除非在草率模式下).阅读有关 this 关键字的更多信息 在 MDN.

If functions (even though they were meant to be methods) are called as plain functions (pushAll()), their this value will be undefined (unless in sloppy mode). Read more on the this keyword at MDN.

这篇关于为什么 apply 尚未绑定到 Javascript 中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆