javascript - Understanding underscore bind -
function checkbalance() { return this.balance; } function person(name, balance) { this.name = name; this.balance = balance; } var me = new person('tim', 1000); _.bind(checkbalance, person); console.log(checkbalance()); //undefined i know case checkbalance should on prototype of person object, i'm failing understand why bind method isn't working here. i've tried both person , me context _.bind bind checkbalance, keep getting undefined. what's going on here i'm getting undefined?
bind(func, obj) returns new function identical func except this inside of function refer obj.
you're binding this in checkbalance function person function, when seems mean bind this me.
try this:
var f = _.bind(checkbalance, me); console.log(f()); //1000 or, reassigning same function:
checkbalance = _.bind(checkbalance, me); console.log(checkbalance()); //1000
Comments
Post a Comment