简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM