我在这里把自己搞糊涂了。我的场景如下。
function DesignPad() {
function EditBar() {
...
this.removeHandler = function() {
**// how do I call Dragger.removeAsset**
}
}
function Dragger(){
...
this.removeAsset = function() {}
}
this.init = function() {
this.editBar = new EditBar();
this.dragger = new Dragger();
}
}
var dp = new DesignPad();
...我好像不能给Dragger.RemoveAsset打电话。我理解为什么,我的问题是我该如何称呼它?
我试图将相似的东西分开(例如Dragger / EditBar),但我似乎在我的事件处理程序中弄混了各种各样的东西。关于这个东西有什么建议,好的阅读材料等吗?
谢谢。
发布于 2009-05-12 19:31:31
我发现Douglas Crockford's Javascript是对JavaScript最好的介绍。特别是雅虎的视频,比如:The JavaScript Programming Language,在那里你可以了解到对象是如何在JS中创建和继承的。
对您的问题的解决方案是:
function DesignPad() {
var that = this;
function EditBar() {
this.removeHandler = function() {
print("RemoveHandler");
that.dragger.removeAsset();
}
}
function Dragger() {
this.removeAsset = function() {
print("RemoveAsset");
}
}
this.init = function() {
this.editBar = new EditBar();
this.dragger = new Dragger();
}
}
var dp = new DesignPad();
dp.init();
dp.editBar.removeHandler();但是,正如其他人注意到的,您可以重构一些东西:)。
发布于 2009-05-12 19:00:04
在我看来,你应该重构这些代码,让它变得更简单。
我认为你的问题来自于嵌套函数是私有的,所以你不能从外部访问它。
发布于 2009-05-12 19:04:38
Dragger的一个实例是你的DesignPad对象的“属性”吗?如果是这样,您可以将对该对象的引用传递到removeHandler()方法中。
https://stackoverflow.com/questions/854269
复制相似问题