对象不能找到方法
Object can't find method
我试图使一个状态机,但它不工作。到目前为止,我已经得到了以下代码:
function makeStateMachine() {
this.stateConstructors = new Object();
this.currState = {
update : function(e) {
// Nothing to do here
},
exit : function() {
// Nothing to declare
}
};
this.nextState = null;
var that = this;
this.update = new function(e) {
that.currState.update(e);
that.changeState();
};
this.setNextState = new function(targetState) {
that.nextState = targetState;
};
this.addState = new function(constructor, stateName) {
that.stateConstructors[stateName] = constructor;
};
this.changeState = new function() {
if (that.nextState != null) {
that.currState.exit();
that.currState = new that.stateConstructors[that.nextState]();
that.nextState = null;
}
};
}
当我尝试运行它时,firebug显示这个错误:"TypeError: that. "changeState不是一个函数",在更新函数的那一行。当我取消对changeState()行的注释时,它开始抱怨EaselJS库不正确(我知道这是正确的,因为它适用于我的其他项目)。有人能帮我一下吗?它可能是一些非常简单的东西(就像往常一样),但我就是找不到错误。如果你们喜欢的话,我可以把剩下的代码贴出来,但我认为这是不相关的。
提前感谢!
您应该将这些函数放在原型中。你也不应该使用= new function(...
;只用= function(...
。最后,你不需要that
。试试下面的代码:
function makeStateMachine() {
this.stateConstructors = {};
this.currState = {
update : function(e) {
// Nothing to do here
},
exit : function() {
// Nothing to declare
}
};
this.nextState = null;
}
makeStateMachine.prototype.update = function(e) {
this.currState.update(e);
this.changeState();
};
makeStateMachine.prototype.setNextState = function(targetState) {
this.nextState = targetState;
};
makeStateMachine.prototype.addState = function(constructor, stateName) {
this.stateConstructors[stateName] = constructor;
};
makeStateMachine.prototype.changeState = function() {
if (this.nextState != null) {
this.currState.exit();
this.currState = new this.stateConstructors[this.nextState]();
this.nextState = null;
}
};
相关文章:
- AngularJS:$q.dedefe()不能由工厂方法共享
- 不能从 Backbone.View 的方法调用 Backbone.collection 的方法,其中包含 require
- 为什么不能在带有条件的ng类中使用方法
- 替换方法不能使用全局修饰符
- 为什么这个jQuery重置方法不能正常工作
- 面向对象的Javascript,为什么对象的方法不能在我的init方法中工作?
- Javascript focus()方法不能与Google Apps Script一起工作
- mootools setProperty方法不能在iphone, ipad上工作
- 为什么我的邮政编码验证方法不能正常工作
- imagesLoaded方法不能与JQuery砌体一起工作
- 无线电组,jQuery next方法不能正常工作
- 数组推送方法不能正常工作
- javascript中的.test()方法不能正常工作
- jQuery find()方法不能只在一个类上工作
- 为什么POST方法不能在移动web应用程序中工作
- Javascript -为什么调用方法不能正常工作
- 为什么直接在函数上添加方法不能像function.prototype那样工作?
- Jquery .clone()方法不能在IE和Chrome浏览器中工作
- JQuery .load()方法不能在Google Chrome浏览器中工作
- Jquery& #39;s html()方法不能有效插入html代码