![](/img/trans.png)
[英]What is the syntax for Function.prototype.call or Function.prototype.apply with an async function
[英]Function.prototype.call and Function.prototype.apply with only one argument
Function.prototype.call或Function.prototype.apply仅使用一个参数做什么?
这里发生了什么?
Function.prototype.myBind = function (context) {
var fun = this;
return function(){
return fun.call(context);
}
}
它在不传递任何参数的情况下调用该函数。
fun.call(context)
fun
的函数将使用context *的context
*进行调用,并且不传递任何参数。
这本质上等效于调用:
context.temp = fun;
context.temp();
当然,通过call
不会添加任何其他属性。
这是一个例子:
var a = {foo: 'bar'}, b = {foo: 'baz'}; function example() { console.log(this.foo); } console.log('example called on a'); example.call(a); //'bar' console.log('example called on b'); example.call(b); //'baz'
* this
在函数里面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.