admin管理员组文章数量:1417426
Just because of curiosity I wanted to make a copy of a function with preset parameters with this particular approach :
var x = function (p1,p2) {
console.log(this, p1, p2);
};
var y = x.bind.bind(this,this,1,2);
y();
So, since "bind" method of a function is a function itself, it has a "bind" method and this "bind" method has another "bind" method and so on ...
But when I run my code in Google Chrome it's giving me
Uncaught TypeError: Bind must be called on a function
So it looks like bind is not a function but
typeof x.bind;
returns
function
So I'm trying to understand what really is the type of bind method!
Is that a bug ?
Just because of curiosity I wanted to make a copy of a function with preset parameters with this particular approach :
var x = function (p1,p2) {
console.log(this, p1, p2);
};
var y = x.bind.bind(this,this,1,2);
y();
So, since "bind" method of a function is a function itself, it has a "bind" method and this "bind" method has another "bind" method and so on ...
But when I run my code in Google Chrome it's giving me
Uncaught TypeError: Bind must be called on a function
So it looks like bind is not a function but
typeof x.bind;
returns
function
So I'm trying to understand what really is the type of bind method!
Is that a bug ?
-
1
x.bind(this).bind(this,1,2);
– pishpish Commented Aug 13, 2016 at 13:48 -
1
What is
this
in your code? It won't be the same asx
if that's what you were hoping.x.bind.bind
is the same asFunction.prototype.bind.bind
, sox
is lost at that point. – user1106925 Commented Aug 13, 2016 at 13:56 -
@Aprillion Yep,
this
from the firstbind
gets overwritten in the second call. I assumed that was the OPs intention, seeing thisx.bind.bind(this,this,1,2);
– pishpish Commented Aug 13, 2016 at 14:00 -
1
@janje You can't overwrite
this
of a "bound" function, ECMAScript 2015:Function.prototype.bind
-> Note 2, fiddle – Andreas Commented Aug 13, 2016 at 14:25 -
1
@Aprillion Same as
var y = Function.prototype.bind.bind(x,this,1,2)(); y();
– pishpish Commented Aug 15, 2016 at 13:54
2 Answers
Reset to default 4You are binding the bind
method to this
, which is not a function. You'll need to bind it to x
which is the actual function that you're going to use bind
on:
var x = function(p1,p2) {
console.log(this, p1, p2);
};
var y = x.bind.bind(x);
var z = y(this, 1, 2); // this call is like `x.bind(this, 1, 2)` now
z();
bind
will return the one previous function definition bound to the context designated by the first argument. So y = x.bind.bind(this,this)
will return the bind function definition bound to this
and eventually it is like y = bind(this)
which will throw the exception that you see.
本文标签: javascriptCalling bind method on bind method of a function in JSStack Overflow
版权声明:本文标题:javascript - Calling bind method on bind method of a function in JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745252632a2649914.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论