JavaScript call()和Prototype - Slice Function [英] JavaScript call() and Prototype - Slice Function

查看:110
本文介绍了JavaScript call()和Prototype - Slice Function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 MDN Article < JavaScript中的/ $> on slice 。我理解除了第二个例子之外的所有内容,标题为类似于对象的对象

I'm reading the MDN Article on slice in JavaScript. I understand everything except the 2nd example in the section titled Array-Like Objects.

它说我们可以通过使<$简化第一个例子c $ c> slice 我们自己的函数如下:

It says we can simplify the first example by making slice our own function as so:

var unboundSlice = Array.prototype.slice;
var slice = Function.prototype.call.bind(unboundSlice);

function list() {
  return slice(arguments);
}

var list1 = list(1, 2, 3); // [1, 2, 3]

我不明白的是 call 可以在第二行 prototype 之后来。

What I don't understand is how call can come right after prototype on the second line.

我通常以 Array.prototype.slice.call(arguments)或其他类似的形式看到它。

I usually see it in the form of Array.prototype.slice.call(arguments) or something of that sort.

我不明白前两行的流程以及它们如何生成这个工作 slice 函数。

I don't understand the flow of the first two lines and how they generate this working slice function.

推荐答案

tl; dr:

var slice = Function.prototype.call.bind(unboundSlice);

是一种简短的写作方式:

is a short way of writing:

var slice = function(value, start, end) {
  return unboundSlice.call(value, start, end);
};






让我们考虑第二行:


Let's think about this line for second:

Array.prototype.slice.call(arguments)

.slice 是一种提取数组子集的数组方法。它的运行价值为 .call 是每个函数都有的方法,它允许您为函数执行设置 this 值。因此,上面的行允许我们执行 slice 作为 arguments 的方法,而不必变异参数本身。我们可以完成

.slice is an array method to extract a subset of the array. It operates on the value of this. .call is a method every function has, it lets you set the this value for a function execution. So, the above line lets us execute slice as a method of arguments, without having to mutate arguments itself. We could have done

arguments.slice = Array.prototype.slice;
arguments.slice();

但不是那么干净。

现在查看

Function.prototype.call.bind(unboundSlice);

如上所述, .call 是一种方法每个功能都有。它还可以在这个上运行,这应该是一个函数。它调用 this 并将该函数的值设置为第一个参数。你可以认为调用类似于

As said, .call is a method that every function has. It also operates on this, which is expected to be a function. It calls this and sets the this value of that function to the first argument. You could think of call as being similar to

function call(thisValue, arg1, arg2, ...) {
   return this.apply(thisValue, [arg1, arg2, ...]);
}

注意它如何调用这个作为函数。

Note how it calls this as a function.

.bind 也是每个函数都有的方法。它返回一个新函数,它的这个值固定在你传入的第一个参数上。

.bind is also a method every function has. It returns a new function which has its this value fixed to the first argument you pass in.

让我们考虑一下 call.bind(unboundSlice)的结果函数如下所示:

Let's consider what the resulting function of call.bind(unboundSlice) would look like:

function boundCall(thisValue, arg1, arg2, ...) {
   return unboundSlice.apply(thisValue, [arg1, arg2, ...]);
}

我们只需更换使用 unboundSlice boundCall 现在将始终调用 unboundSlice

We simply replaced this with unboundSlice. boundCall will now always call unboundSlice.

这篇关于JavaScript call()和Prototype - Slice Function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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