Javascript - 引用实例化对象
Javascript - referring to the instantiated object
假设我有一个 javascript 函数,如下所示:
car.prototype = {
this.stolen = "",
initialize: function(){
this.stolen = false;
},
steal: function(){
Event.observe(window, "resize", function(){
this.stolen = true;
});
}
}
在steal
方法中,如何在Event.observe()
方法中引用car
对象的stolen
属性?在上面的代码中,Event.observe()
方法中的 this
指的是 window 而不是 car
对象。
你bind函数:
Event.observe(window, "resize", (function(){
this.stolen = true;
}).bind(this));
假设我有一个 javascript 函数,如下所示:
car.prototype = {
this.stolen = "",
initialize: function(){
this.stolen = false;
},
steal: function(){
Event.observe(window, "resize", function(){
this.stolen = true;
});
}
}
在steal
方法中,如何在Event.observe()
方法中引用car
对象的stolen
属性?在上面的代码中,Event.observe()
方法中的 this
指的是 window 而不是 car
对象。
你bind函数:
Event.observe(window, "resize", (function(){
this.stolen = true;
}).bind(this));