jremi
jremi

Reputation: 2969

JS Best practice for passing argument with bind to function inside object to access specific variable inside objects scope

If passing into calculate(z) on object a would the bind be considered proper "best practice" for scoping x ?

var a = {
    x: 10,
    calculate: function(z){
        return this.x + this.y + z;
    }
};

a.calculate.bind({x:a.x, y:3})(10); // 23

Upvotes: 0

Views: 100

Answers (1)

Bergi
Bergi

Reputation: 665020

No, using a.calculate.bind({x:a.x, y:3})(10) is not a best practice. You're looking for .call():

a.calculate.call({x:a.x, y:3}, 10)

Upvotes: 2

Related Questions