arrays - How to destroy an object in javascript? - Stack Overflow

I'm wondering how to properly destroy an object in Javascript that contains an array among other d

I'm wondering how to properly destroy an object in Javascript that contains an array among other data.

For example (this is the initial state of the object):

acme.models.nomination = {
    recipients : [], // array will be added to later.
    awardType: {}, // this object will be filled out later.
    privacy: 'private',
    ...
};

As the application is used the nomination.recipients object will be added to, along with the other data elements. Is it enough to do the following to clear the plete object when finished with it:

acme.models.nomination = {};

or is this better (or overkill):

acme.models.nomination.privacy = undefined;
acme.models.nomination.awardType = {};
acme.models.nomination.recipients = {};
acme.models.nomination = {};

I'm thinking that the former statement (i.e. acme.models.nomination = {}) is enough as this effectively makes the entire contents unreachable and hence eligible for garbage collection. However, I'd like to get other peoples opinions on this. BTW, the answer can be given in a modern browser context, let's say browsers post-Jan-2012, as I know memory management was a bit inconsistent in the past.

I'm wondering how to properly destroy an object in Javascript that contains an array among other data.

For example (this is the initial state of the object):

acme.models.nomination = {
    recipients : [], // array will be added to later.
    awardType: {}, // this object will be filled out later.
    privacy: 'private',
    ...
};

As the application is used the nomination.recipients object will be added to, along with the other data elements. Is it enough to do the following to clear the plete object when finished with it:

acme.models.nomination = {};

or is this better (or overkill):

acme.models.nomination.privacy = undefined;
acme.models.nomination.awardType = {};
acme.models.nomination.recipients = {};
acme.models.nomination = {};

I'm thinking that the former statement (i.e. acme.models.nomination = {}) is enough as this effectively makes the entire contents unreachable and hence eligible for garbage collection. However, I'd like to get other peoples opinions on this. BTW, the answer can be given in a modern browser context, let's say browsers post-Jan-2012, as I know memory management was a bit inconsistent in the past.

Share Improve this question edited Jul 2, 2012 at 11:56 Philip Murphy asked Jul 2, 2012 at 11:39 Philip MurphyPhilip Murphy 8303 gold badges10 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

I think the delete-Operator is what you are looking for:

delete acme.models.nomination;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745172691a4615021.html

相关推荐

  • arrays - How to destroy an object in javascript? - Stack Overflow

    I'm wondering how to properly destroy an object in Javascript that contains an array among other d

    1小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信