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 ?

Share Improve this question edited Aug 13, 2016 at 14:36 mmvsbg 3,58817 gold badges54 silver badges74 bronze badges asked Aug 13, 2016 at 13:46 John DoeJohn Doe 31 silver badge4 bronze badges 9
  • 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 as x if that's what you were hoping. x.bind.bind is the same as Function.prototype.bind.bind, so x is lost at that point. – user1106925 Commented Aug 13, 2016 at 13:56
  • @Aprillion Yep, this from the first bind gets overwritten in the second call. I assumed that was the OPs intention, seeing this x.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
 |  Show 4 more ments

2 Answers 2

Reset to default 4

You 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