在javascript中调用并应用 [英] call and apply in javascript
问题描述
可能重复:
链接呼叫和一起申请的含义是什么?
我找到了这样的代码:
function fun() {
return Function.prototype.call.apply(Array.prototype.slice, arguments);
}
我知道电话
和在js中应用
,但是当他们走到一起时我很困惑。
I know the call
and apply
in js,however I am confused when they come together.
然后我想知道是否
Function.prototype.call.apply(Array.prototype.slice, arguments)
与以下内容相同:
Array.prototype.slice.apply(arguments);
如果没有,第一行会做什么?
If not,what does the first line do?
推荐答案
好吧,让我们通过替换来解决这个问题。我们从:
Alright, let's tackle this problem via substitution. We start with:
Function.prototype.call.apply(Array.prototype.slice, arguments);
我们所知道的:
-
Function.prototype.call
是一个函数。 -
this
指针调用
指向Function.prototype
。 - 我们使用
apply
将指针更改为
code> Array.prototype.slice 。 -
参数
已应用(未作为参数传递)到调用
。
Function.prototype.call
is a function.- The
this
pointer ofcall
points toFunction.prototype
. - We use
apply
to change thethis
pointer ofcall
toArray.prototype.slice
. arguments
is applied (not passed as a parameter) tocall
.
因此上面的语句相当于:
Thus the above statement is equivalent to:
Array.prototype.slice.call(arguments[0], arguments[1], ...);
从此我们看到:
-
Array.prototype.slice
是一个函数。 -
this
指针切片
指向Array.prototype
。 - 我们使用
调用
将此
指针slice
更改为arguments [0]
。 -
arguments [1],...
作为参数传递给切片
。
Array.prototype.slice
is a function.- The
this
pointer ofslice
points toArray.prototype
. - We use
call
to change thethis
pointer ofslice
toarguments[0]
. arguments[1], ...
are passed as parameters toslice
.
这与以下内容相同:
arguments[0].slice(arguments[1], ...);
这样做的好处是我们正在创建一个 slice的Stack Overflow>快速未绑定包装器在一行中。
The advantage of this is that we're creating a fast unbound wrapper for slice
in a single line.
编辑:创建快速未绑定包装器的更好方法如下(请注意,它可能不起作用一些旧的浏览器,但你现在不需要担心 - 你可能总是使用 shim 适用于不支持 bind
)的浏览器:
A better way to create fast unbound wrappers is as follows (note that it may not work in some older browsers, but you don't really need to worry about that now - you may always use a shim for browsers which don't support bind
):
var slice = Function.prototype.call.bind(Array.prototype.slice);
这与以下内容相同:
function slice() {
return Function.prototype.call.apply(Array.prototype.slice, arguments);
}
工作原理:
-
Function.prototype.call
是一个函数。 -
此
指针调用
指向Function.prototype
。 - 我们使用
bind
更改此
指针的调用
到Array.prototype.slice
。 -
bind
返回一个参数
的已应用到的函数
。
Function.prototype.call
is a function.- The
this
pointer ofcall
points toFunction.prototype
. - We use
bind
to change thethis
pointer ofcall
toArray.prototype.slice
. bind
returns a function whosearguments
are applied tocall
.
奖励:如果您的编程风格功能强大,就像我的一样,那么您会发现这段代码非常有用:
Bonus: If your style of programming is highly functional, like mine is, then you would find that is piece of code is very useful:
var funct = Function.prototype;
var obj = Object.prototype;
var arr = Array.prototype;
var bind = funct.bind;
var unbind = bind.bind(bind);
var call = unbind(funct.call);
var apply = unbind(funct.apply);
var classOf = call(obj.toString);
var ownPropertyOf = call(obj.hasOwnProperty);
var concatenate = call(arr.concat);
var arrayFrom = call(arr.slice);
- 使用此功能,您可以使用<$ c $轻松创建未绑定的包装器c>致电或
申请
。 - 您可以使用
classOf
获取值的内部[[Class]]
。 - 您可以使用
ownPropertyOf
in for for loops。 - 您可以使用
concatenate
来连接数组。 - 您可以使用
arrayFrom
来创建数组。
- Using this you may easily create unbound wrappers using either
call
orapply
. - You may use
classOf
to get the internal[[Class]]
of a value. - You may use
ownPropertyOf
inside for in loops. - You may use
concatenate
to join arrays. - You may use
arrayFrom
to create arrays.
这篇关于在javascript中调用并应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!