bind: 绑定

很多时候,需要为某个函数指定一个固定的 this 对象,最简单的方式即是使用闭包来获取一个不变的 this 对象。
this.x = 9; const module = { x: 81, getX: function() { return this.x; } }; module.getX(); // 81 const getX = module.getX; getX(); // 9, because in this case, "this" refers to the global object // Create a new function with 'this' bound to module const boundGetX = getX.bind(module); boundGetX(); // 81

猜你喜欢

转载自www.cnblogs.com/aisiqi-love/p/11796521.html