如何实现析构函数? [英] How to implement a destructor?

查看:57
本文介绍了如何实现析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在寻找一种方法来实现析构函数作为

类的成员方法。


/ * BEGIN CODE * /


函数SomeClass(){


this.constructor.numInstances ++;


this.destroy = function(){


this.constructor.numInstances--;

/ *

需要在这里删除实例

* /


};

}


SomeClass.numInstances = 0;


sc = new SomeClass();


// SomeClass.numInstances = 1


sc.destroy();


// SomeClass.numInstances = 0且sc不再被引用


/ * END CODE * /


如果只涉及删除操作(如 - 删除sc - in

上面的例子),我不需要一个析构函数,但是因为

一个操作更多(减少实例的数量;并且那里

可能更多),而且我不想将完全销毁

进程委托给实例化对象的程序,我需要实现一个析构函数
作为会员方法。


任何帮助都将在此得到赞赏。


F.

Hi,

I''m looking for a way to implement a destructor as a member method of a
class.

/* BEGIN CODE */

function SomeClass() {

this.constructor.numInstances++;

this.destroy = function () {

this.constructor.numInstances--;
/*
Need someway here to delete the instance
*/

};
}

SomeClass.numInstances = 0;

sc = new SomeClass();

// SomeClass.numInstances = 1

sc.destroy();

// SomeClass.numInstances = 0 and sc is not referenced anymore

/* END CODE */

If there was only a delete operation involved (as in -- delete sc -- in
the above exemple) , I wouldn''t need a destructor, but since there is
one operation more (decrementing the number of instances; and there
could be more), and I do not want to delegate the full destruction
process to the procedure which instantiated the object, I need to
implement a destructor as a member method.

Any help would be appreciated on this.

F.

推荐答案

Fran?ois写道:
Fran?ois wrote:


我正在寻找一种方法来实现析构函数一个
类的成员方法。
Hi,

I''m looking for a way to implement a destructor as a member method of a
class.



为什么? JavaScript中的删除与Java或C ++中的删除不同。


-

Ian Collins。


Why? delete in JavaScript isn''t the same as in Java or C++.

--
Ian Collins.


Ian,


我需要实现它以便我可以删除一个实例并在

同时提供一系列由破坏:


sc.destroy() - >删除sc和 - >减少实例数

和 - >做一些其他动作(触发事件,弹出警报(

对象sc已被销毁)或其他一些动作,

破坏的所有部分过程)


我可以在课堂外完成所有这些,在调用程序中:


sc = new SomeClass();

删除sc;

SomeClass.constructor.numInstances--;

event = new CustomEvent();

alert(" ;对象sc已经被破坏了);


在我看来它会破坏封装:SomeClass类

在它的边界外流血。 />

感谢您的回复


F.

Ian,

I need to implement it so I can delete an instance and provide at the
same time a chain of actions triggered by the destruction:

sc.destroy() --> deletes sc and --> decrements the number of instances
and --> do some other action (triggers an event, pops up an alert("The
object sc has been destroyed") or some other actions, all part of the
destruction process)

I could do all this outside the class, in the calling procedure:

sc = new SomeClass();
delete sc;
SomeClass.constructor.numInstances--;
event = new CustomEvent();
alert("The object sc has been destroyed");

It seems to me that it would break encapsulation: the SomeClass class
bleeds outsides its boundaries.

Thanks for the reply

F.


Ian,


我需要实现它所以我可以删除一个实例并在

同时提供由销毁触发的一系列动作:


sc.destroy() - >删除sc和 - >减少实例数

和 - >做一些其他动作(触发事件,弹出警报(

对象sc已被销毁)或其他一些动作,

破坏的所有部分过程)


我可以在课堂外完成所有这些,在调用程序中:


sc = new SomeClass();

删除sc;

SomeClass.constructor.numInstances--;

event = new CustomEvent();

alert(" ;对象sc已经被破坏了);


在我看来它会破坏封装:SomeClass类

在它的边界外流血。 />

感谢您的回复


F.

Ian,

I need to implement it so I can delete an instance and provide at the
same time a chain of actions triggered by the destruction:

sc.destroy() --> deletes sc and --> decrements the number of instances
and --> do some other action (triggers an event, pops up an alert("The
object sc has been destroyed") or some other actions, all part of the
destruction process)

I could do all this outside the class, in the calling procedure:

sc = new SomeClass();
delete sc;
SomeClass.constructor.numInstances--;
event = new CustomEvent();
alert("The object sc has been destroyed");

It seems to me that it would break encapsulation: the SomeClass class
bleeds outsides its boundaries.

Thanks for the reply

F.


这篇关于如何实现析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆