繁体   English   中英

JS在对象内部传递带有绑定的参数以访问对象范围内特定变量的最佳实践

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

如果传递到对象a的calculate(z),绑定将被视为确定x的适当“最佳实践”吗?

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

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

不,使用a.calculate.bind({x:ax, y:3})(10)不是最佳做法。 您正在寻找.call()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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