var x = 9;
var module = {
x: 81,
getX: function() { return this.x; }
};
module.getX(); // 81
var getX = module.getX;
getX(); // 9, because in this case, "this" refers to the global object
// create a new function with 'this' bound to module
var boundGetX = getX.bind(module);
boundGetX(); // 81
something about bind
最新推荐文章于 2026-07-03 14:09:26 发布
本文通过一个具体的JavaScript示例,深入探讨了`this`关键字在不同上下文中如何绑定的问题。特别是当`this`作为全局对象与作为特定对象的属性时的行为差异,以及如何使用`.bind()`方法来改变`this`的绑定。
584

被折叠的 条评论
为什么被折叠?



