最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

memory management - WhenIf to use Delete in Javascript - Stack Overflow

programmeradmin0浏览0评论

I just found out that javascript has a delete statement. I've read a bit about it and am not much the wiser.

So I am hoping to get a functional definition of when I should use it, if at all. So I know I can delete properties of an object; as is made obvious by this fiddle:

var myData = {a:"hello",b:"world"};
alert(myData.b);
delete myData.b;
alert(myData.b);

Which shows "world" then undefined in successive alerts. However, you cannot use delete like this (as one might in C++):

function data() {
    this.attribute1 = "aww";
    this.attribute2 = "poo";
}

var myData = new data();
delete myData;

Here delete returns false indicating that you cannot delete myData. I used to work primarily in C++ and this was like the whole idea of delete. I can't think of any reason I would use delete to remove properties. Should I ever worry about using delete to mark memory to be freed? Like if I do something like this.

var myData = new data();
... //do stuff
myData = new data();

Addition

So I dug up the post that confused me. The most upvoted answer on this question states (as quoted from the Apple Javascript Coding Guidelines):

Use delete statements. Whenever you create an object using a new statement, pair it with a delete statement. This ensures that all of the memory associated with the object, including its property name, is available for garbage collection. The delete statement is discussed more in “Freeing Objects.”

So, if I understand some of the ments and answers I've been given, this statement is not accurate, because you cannot even call delete on an object created using a new statement.

I just found out that javascript has a delete statement. I've read a bit about it and am not much the wiser.

So I am hoping to get a functional definition of when I should use it, if at all. So I know I can delete properties of an object; as is made obvious by this fiddle:

var myData = {a:"hello",b:"world"};
alert(myData.b);
delete myData.b;
alert(myData.b);

Which shows "world" then undefined in successive alerts. However, you cannot use delete like this (as one might in C++):

function data() {
    this.attribute1 = "aww";
    this.attribute2 = "poo";
}

var myData = new data();
delete myData;

Here delete returns false indicating that you cannot delete myData. I used to work primarily in C++ and this was like the whole idea of delete. I can't think of any reason I would use delete to remove properties. Should I ever worry about using delete to mark memory to be freed? Like if I do something like this.

var myData = new data();
... //do stuff
myData = new data();

Addition

So I dug up the post that confused me. The most upvoted answer on this question states (as quoted from the Apple Javascript Coding Guidelines):

Use delete statements. Whenever you create an object using a new statement, pair it with a delete statement. This ensures that all of the memory associated with the object, including its property name, is available for garbage collection. The delete statement is discussed more in “Freeing Objects.”

So, if I understand some of the ments and answers I've been given, this statement is not accurate, because you cannot even call delete on an object created using a new statement.

Share Improve this question edited May 23, 2017 at 11:54 CommunityBot 11 silver badge asked Nov 11, 2015 at 20:47 IanIan 4,4774 gold badges41 silver badges69 bronze badges 7
  • if you want to 'delete' an entire object then set it to null i.e. myData = null; – user2417483 Commented Nov 11, 2015 at 20:56
  • (As long as nothing else has a reference to the same data.) – Dave Newton Commented Nov 11, 2015 at 20:57
  • Here's a really good treatise on delete in JS: perfectionkills./understanding-delete – Rick Visi Commented Nov 11, 2015 at 21:03
  • @jeff - So I've got a few large array variables that I reset at different points in my code without worrying about the data that used to be pointed to. Should I explicitly set those variables to null before resetting them to the new data? Like so: myData = ["a","b"]; myData = null; myData = ["c","d"]; ? – Ian Commented Nov 11, 2015 at 21:04
  • @Ian: That question was posted over 6 years ago. JavaScript has changed since then, and what is may not necessarily be what was. It very could have been accurate in 2009 - I can't speak on that as I wasn't even in college at that point. – Bardicer Commented Nov 11, 2015 at 21:06
 |  Show 2 more ments

2 Answers 2

Reset to default 4

According to mozilla's developer documents, delete does not work that way.

The delete operator deletes a property from an object, it does not delete the object itself.

So instead of using it as you have demonstrated, you would use it more like the following:

myGlobalObject = {};
var myObject = {};
myObject.propertyA = "blah";

// Do some stuff

delete myObject.propertyA; // This works because you're deleting a property off myObject
delete myGlobalObject; // This works because myGlobalObject is a property of the global object.
delete myObject; // This does NOT work - most likely because you declared it using the var keyword

This doesn't actually do garbage collection though. Also if myObject has a prototype up the chain that has propertyA it would still inherit that property through the prototype.

For more indepth information feel free to check out the developer documents:

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/delete

delete on its own specifically states:

The delete operator removes a property from an object.

You might remove a property if you don't want it included in data sent to a server, or used by other code, e.g., something that automatically takes object data and turns it into a table.

In general you'd never use it for memory management, a possible exception being if you had a huge chunk of data in an object (like received from the back end) that you explicitly don't need.

发布评论

评论列表(0)

  1. 暂无评论