控制在 MooTools 中创建的对象数量

发布于 2024-12-23 14:35:55 字数 737 浏览 1 评论 0原文

有没有办法统计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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱,才寂寞 2024-12-30 14:35:55

您可以使用 Events 类作为混合,以便它通知工厂动物的死亡...

var Animal = new Class({

    Implements: [Events,Options], // mixin

    initialize: function(options){
        this.setOptions(options);
    },
    create: function() {
        alert('created!');
        this.fireEvent("create");
    },
    destroy: function() {
        alert('destroyed');
        this.fireEvent("destroy", this); // notify the instance
    }
});

var AnimalFactory = new Class({
    animalsContainer: [],
    initialize: function() {
        var self = this;
        for(i=0;i<10;i++) {
            this.add(new Animal({
                onDestroy: this.deleteA.bind(this)
            }));
        }
    },
    add: function(animal) {
        this.animalsContainer.push(animal);
    },
    deleteA: function(animal) {
        this.animalsContainer[this.animalsContainer.indexOf(animal)] = null;
        animal = null; // gc
    }
});


var foo = new AnimalFactory();
console.log(foo.animalsContainer[0]);
foo.animalsContainer[0].destroy();
console.log(foo.animalsContainer[0]);

观察它的运行: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...

var Animal = new Class({

    Implements: [Events,Options], // mixin

    initialize: function(options){
        this.setOptions(options);
    },
    create: function() {
        alert('created!');
        this.fireEvent("create");
    },
    destroy: function() {
        alert('destroyed');
        this.fireEvent("destroy", this); // notify the instance
    }
});

var AnimalFactory = new Class({
    animalsContainer: [],
    initialize: function() {
        var self = this;
        for(i=0;i<10;i++) {
            this.add(new Animal({
                onDestroy: this.deleteA.bind(this)
            }));
        }
    },
    add: function(animal) {
        this.animalsContainer.push(animal);
    },
    deleteA: function(animal) {
        this.animalsContainer[this.animalsContainer.indexOf(animal)] = null;
        animal = null; // gc
    }
});


var foo = new AnimalFactory();
console.log(foo.animalsContainer[0]);
foo.animalsContainer[0].destroy();
console.log(foo.animalsContainer[0]);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文