[英]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.