Why javascript bind doesn't work
It does work, talk.bind(p) returns the bound function:
talk.bind(p)();
Nothing is wrong with bind()
-- it's just not being used correctly. bind()
returns a new function which is bound to the object specified. You still need to execute that function:
function talk(){
console.log(this.name + " dice: ");
}
var Person = function(name, surname){
this.name = name;
this.surname = surname;
}
var p = new Person("Mark", "Red");
var markTalks = talk.bind(p);
markTalks(); // logs properly
There is nothing wrong with bind
, it returns a function which is bound to the object passed as argument. So you need to invoke it like this
talk.bind(p)();