Javascript Function.prototype.call() [英] Javascript Function.prototype.call()

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

问题描述

  fn.call(thisValue) ; 
Function.prototype.call.call(fn,thisValue);

对于第1行,我的理解是Javascript中的每个函数对象都有一个方法<$ c从 Function.prototype 继承的$ c> call 以及 call 方法的作用是 fn 函数定义中的 this 关键字的值为 thisValue (我在调用方法中传递的第一个参数。 fn 是一个函数,所以我在 fn.call(thisValue) code>只是调用 fn ,并将函数内的 this 关键字设置为 thisValue



但是对于第2行,我不明白。有人可以帮助解释第2行正在做什么。

解决方案

让我们从这个设置开始:

 函数fn(){console.log(this);} 
var thisvalue = {fn:fn};

现在你一定明白 thisvalue.fn()是一个方法调用,并将记录的 this 值设置为 thisvalue 对象。



接下来,您似乎知道 fn.call(thisvalue)完成相同的调用。或者,我们可以写(thisvalue.fn).call(thisvalue)(括号仅用于结构,可以省略)如 thisvalue.fn == = fn

  thisvalue.fn(...); //相当于
(thisvalue.fn).call(thisvalue,...); //或:
(fn).call(thisvalue,...);

好的,但是 fn.call(...)也只是一个方法调用 - <函数中的code> call 方法 fn 函数上调用。

它可以是因为所有的函数对象都从 Function.prototype 继承这个 .call 属性 - 它不是像 .fn 放在 thisvalue 对象上。但是, fn.call === Function.prototype.call thisvalue.fn === fn



现在,我们可以将 .call 的方法调用重写为 .call():

  fn.call(thisvalue); //相当于
(fn.call).call(fn,thisvalue); //或:
(Function.protoype.call).call(fn,thisvalue);

我希望你发现这个模式,现在可以解释下面的工作原因:

  Function.prototype.call.call.call(Function.prototype.call,fn,thisvalue); 
var call = Function.prototype.call; call.call(call,fn,thisvalue);

打破这种情况只会给读者一个练习: - )


I read some article and it said the following 2 line are doing the same thing.

fn.call(thisValue);
Function.prototype.call.call(fn, thisValue);

For line 1, my understanding is that every function object in Javascript do have a the method call inherited from the Function.prototype and what call method does is to have the this keyword inside the function definition of fn to be thisValue(the first parameter I passed in the call method. fn is a function so what I am doing in fn.call(thisValue) is just invoking fn and set the this keyword inside the function to be thisValue.

But For line 2, I don't get it. Can someone help to explain it what the hack the line 2 is doing.

解决方案

Let's start with this setup:

function fn() { console.log(this); }
var thisvalue = {fn: fn};

Now you surely understand that thisvalue.fn() is a method call, and sets the logged this value to the thisvalue object.

Next, you seem to know that fn.call(thisvalue) does exactly the same call. Alternatively, we could write (thisvalue.fn).call(thisvalue) (parentheses just for structure, could be omitted) as thisvalue.fn === fn:

thisvalue.fn(…); // is equivalent to
(thisvalue.fn).call(thisvalue, …); // or:
(fn).call(thisvalue, …);

OK, but fn.call(…) is just a method call as well - the call method of functions is called on the fn function.
It can be accessed as such because all function objects inherit this .call property from Function.prototype - it's not an own property like .fn on the thisvalue object. However, fn.call === Function.prototype.call is the same as thisvalue.fn === fn.

Now, we can rewrite that method call of .call as an explicit invocation with .call():

fn.call(thisvalue); // is equivalent to
(fn.call).call(fn, thisvalue); // or:
(Function.protoype.call).call(fn, thisvalue);

I hope you spot the pattern and can now explain why the following work as well:

Function.prototype.call.call.call(Function.prototype.call, fn, thisvalue);
var call = Function.prototype.call; call.call(call, fn, thisvalue);

Breaking this down is left as an exercise to the reader :-)

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

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