mootools析构函数
问题描述:
mootools是否具有析构函数?我有一个静态变量来计算类的实例。
问题是,当一个实例被破坏,我无法更新我的静态变量。 有没有办法扩展析构函数,所以我有更新该var的可能性?mootools析构函数
答
从未见过的mootools这个工作,通常情况下,你让浏览器垃圾回收等等...
这是迄今为止没有一个理想的解决方案 - 它需要知道该实例的范围(窗口,其他对象等等)。
混合类:
var Destructor = new Class({
destruct: function(scope) {
scope = scope || window;
// find the object name in the scope
var name = Object.keyOf(scope, this);
// let someone know
this.fireEvent && this.fireEvent('destroy');
// remove instance from parent object
delete scope[name];
}
});
你再使用它的类,你想:
var a = new Class({
Implements: [Events, Options, Destructor],
initialize: function(options) {
this.setOptions(options);
this.hai();
},
hai: function() {
console.log('hai');
}
});
最后,你创建类的实例绑定到onDestroy
var instance = new a({
onDestroy: function() {
console.log('goodbye cruel world. time to set affairs in order!');
}
});
instance.destruct();
instance.hai(); // reference error.
事件
我知道这很冒险,但它可能会让你明智地摧毁课程并做cl eanup。
非常感谢您的解决方案。我想我会使用它,直到mootools允许我们使用特定的析构函数。 – user1552480 2012-07-27 11:57:09