控制在 MooTools 中创建的对象数量
有没有办法统计mootools中创建和销毁的对象数量?
假设这种情况:
var Animal = new Class({
initialize: function(){},
create: function() {
alert('created!');
},
destroy: function() {
alert('destroyed');
}
});
var AnimalFactory = new Class({
initialize: function() {
for(i=0;i<10;i++) {
this.add(new Animal());
}
},
add: function(animal) {
this.animalsContainer.push(animal);
},
delete: function(animal) {
this.animalsContainer.remove(animal);
}
});
var animalFactory = new AnimalFactory();
我知道一开始创建了多少动物,但是想象一下在代码中的某个地方调用了具体动物实例的动物销毁函数(此处未显示代码)。我怎样才能使animalContainer数组少一个就正确更新?
任何帮助将不胜感激。
谢谢!!
Is there a way to count the number of objects created and destroyed in mootools?
Suppose this case:
var Animal = new Class({
initialize: function(){},
create: function() {
alert('created!');
},
destroy: function() {
alert('destroyed');
}
});
var AnimalFactory = new Class({
initialize: function() {
for(i=0;i<10;i++) {
this.add(new Animal());
}
},
add: function(animal) {
this.animalsContainer.push(animal);
},
delete: function(animal) {
this.animalsContainer.remove(animal);
}
});
var animalFactory = new AnimalFactory();
I know how many animals I have created at the beginning but, imagine that somewhere in the code the animal destroy function from a concrete animal instance is called (code not shown here). how can i make the animalContainer array update correctly with one less?
Any help will be much appreciated.
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
Events
类作为混合,以便它通知工厂动物的死亡...观察它的运行:http://jsfiddle.net/dimitar/57SRR/
这是试图保持数组的索引/长度完整,以防您保存它们
You can use the
Events
Class as a mix-in so that it notifies the factory of the animal's demise...watch it run: http://jsfiddle.net/dimitar/57SRR/
this is trying to keep the indexes/length of the array intact in case you save them