JS原型类并非所有属性都可访问
JS prototype class not all properties are accessible
我定义了一个原型类,它几乎没有属性和方法,由于某些原因,在某些情况下(主要是涉及回调时)我无法访问所有属性。
这是我的代码(删除了一些行以使其更清晰)
var Lobby = function (preloader, serverConn) {
// Hold a reference to EventBus
this.serverConn = serverConn;
this.preloader = preloader;
this.session_id = null;
this.scheduleItemService = new ScheduledItemService(this.preloader);
// Instantiate lobby stage
this.stage = new createjs.Stage("lobbyCanvas");
};
Lobby.prototype._renderLobbyImages = function(stage, scheduleItemService) {
// at this point, 'stage' and 'scheduleItemService' are valid!
..
// onScheduledItemClick callback function
this.scheduleItemService.render(stage, this._onScheduledItemClick);
}
Lobby.prototype._onScheduledItemClick = function(event) {
(function(inst) {
inst.serverConn.asyncSend(
'game.lobby',
{ 'action' : 'claim_scheduled_item',
'session_id' : inst.session_id, **//Here, 'inst.session_id' is DEFINED!**
},
function (error, reply) {
**// Here, both 'stage' and 'scheduleItemService' are UNDEFINED! WHY?!?!**
inst.scheduleItemService.startCountdown(inst.stage);
}
});
})(this);
}
ScheduledItemService.prototype.render = function(stage, onScheduledItemClick) {
var scheduled_item = new createjs.Bitmap(preloader.getResult("scheduled_item_img"));
..
..
scheduled_item.addEventListener('click', onScheduledItemClick);
..
}
为什么this.session_id在"this"中定义,而"stage"answers"scheduleItemService"未定义??这3个是大堂类的属性。。。
我尝试在"this"指针中浏览,但确实找不到"stage"answers"scheduleItemService"。
我错过了什么,对吧?
谢谢:-)
此处的scheduleItemService
变量超出范围
function (error, reply) {
scheduleItemService.startCountdown(stage);
}
问题是,即使你没有使用Promises,这个代码仍然会失败。如果你想将renderLobbyImages
和onScheduledItemClick
定义为原型方法,你必须编写
Lobby.prototype.renderLobbyImages = function(){
// this refers to a Lobby instance
this.scheduleItemService.render(stage, this.onScheduledItemClick);
}
Lobby.prototype.onScheduledItemClick = function(){
// this refers to a lobby instance
}
您还必须在onScheduledItemClick
中使用use this
关键字。
然后,在您定义的promise回调中,"this
"关键字不会指向实例。这就是您的代码出现错误的原因。在这个回调中,this
发生了变化。
为了解决这个问题,在回调之前,将一个临时变量存储到"this
,在这里我将其命名为scope
"。您可以像使用this
一样使用此scope
。
Lobby.prototype.onScheduledItemClick = function(event) {
// this refers to a lobby instance
var scope = this;
this.serverConn.asyncSend(
'game.lobby',
{
'action' : 'claim_scheduled_item',
'session_id' : scope.session_id.bind(scope), // Here scope instead of this. But you could use this as not inside callback. I used scope just for consistency
},
function (error, reply) {
// this does NOT refer to a lobby instance. You must use scope
scope.scheduleItemService.startCountdown(stage);
}
});
编辑1
在您的代码编辑之后,我仍然可以看到一些错误。
我看到在您的Lobby.prototype._onScheduledItemClick
中,您使用的是(inst)
变量this.serverConn.asyncSend
,它应该是inst.serverConn.asyncSen
--
编辑2
代码的另一个问题是回调。该范围未传递。您必须将回调与作用域"绑定"。这是使用函数.bind();
所以现在,你的线路看起来像:
this.scheduleItemService.render(stage, this._onScheduledItemClick.bind(this));
这样,当调用回调时,"this
"变量将具有您在bind
中传递的参数的值。
首先,您没有在您的东西前面加上this
。与其他一些语言不同,this
在JavaScript中不是可选的。
当您输入一个新的函数调用时,this
将被重新定义。您将需要保存对它的引用。
CCD_ 21被定义为被调用函数的父函数。因此,每次调用函数时,它都会发生变化。以下是人们经常做的事情:
Something.prototype.foo = function() {
var self = this;
doSomething(function() {
// now this has changed, but self still references the original this
});
};
- JavaScript Pub/Sub属性访问问题
- AngularJS:如何用同一对象的另一个属性访问一个属性
- Ember JS属性访问器
- 对方法或属性访问的意外调用
- 对jQuery中IE8的方法或属性访问的意外调用
- 对方法或属性访问的意外调用.在JQUERY
- XDomainRequest onload上对方法或属性访问的意外调用
- Javascript属性访问速度:var.properties与var[“属性”]的区别
- 从函数属性访问对象属性;不适用于Firefox
- 重写内部 HTML 属性访问
- (仅在IE中)SCRIPT65535:对方法或属性访问的意外调用.主.js,第 152 行字符 28
- 使用表达式属性访问不同的模型模板选项
- jQuery mobile 1.1.0 滑块:JS-对象属性访问
- Ractive.js,通过命名属性访问父属性
- jQuery验证插件addMethod可以通过属性访问
- 如何使用动态属性访问对象
- 我无法通过 Firefox 中的“for”属性访问标签,但可以在 Chrome 中访问标签
- JavaScript 对象原型属性访问
- 使用虚拟属性访问猫鼬模型中的嵌套文档
- 是否可以在Javascript/Coffeescript中的属性访问期间自动调用函数调用