JavaScript new keyword and memory management

Coming from C++ it is hard grained into my mind that everytime I call new I call delete . In JavaScript I find myself calling new occasionally in my code but (hoping) the garbage collection functionality in the browser will take care of the mess for me.

I don't like this - is there a delete method in JavaScript and is how I use it different from in C++?

Thanks.


var component = new Component();
component = null; // delete this at next garbage collection

Incidentally, the "new" keyword isn't really necessary in javascript, and has nothing (directly) to do with allocating memory. All "new" does is pass a new, empty object called "this" (this = {}) as a hidden argument to the function.

var MyClass = function(){
    // fresh empty object "this" gets passed to function
    // when you use the "new" keyword 
    this.method = function(){}
}

var myInstance = new MyClass();

Javascript can get kind of hairy with nested closures and multiple "this" variables floating around in different scopes. I prefer to do it this way:

var MyNoNewClass = function(){
    // I find this more explicit and less confusing
    var self = {}
    self.method = function(){}
    return self;
}

var myNoNewInstance = MyNoNewClass()

All JavaScript memory is referenced, but not in the traditional sense. Memory is referenced not by memory address but by a string. In this code:

var x = new someObj();

That object is referenced by the string "x" from that point forward. x is not a pointer to some memory on the heap at that point. If you assigned xa property then:

x.someProp = 42;

Then someProp is a string in memory referencing the value 42. Consequently that lets you use array notation to access it by it's string representation:

x["someProp"]++;

It's also why variables can hold any value as they don't have need of size.

Memory is collected in JavaScript, effectively, when no more strings (aka variables or property names) are referencing it. That object will be collected when x is assigned any other value. You could set it to null , undefined , or anything else and that memory will be collected.

That is, it will be collected when the browser or whatever JavaScript engine gets around to it.

Delete only removes properties from objects. From then on attempting to access that property will return undefined . For the most part, the following 2 lines of code are equivalent:

x["someProp"] = undefined;
delete x.someProp;

Edit: Ok, internally the two lines aren't the same. The delete operator will remove the "someProp" reference from memory, while setting it to undefined won't. I think. I can't find anything in the specs about setting a variable or property to undefined, but I don't think doing so does anything special.

The important thing to note is that you won't be able to delete properties that have a certain flag set, but you can set them to null or undefined (if they're not wrapped by a setter and even allow that to happen).

链接地址: http://www.djcxy.com/p/27170.html

上一篇: 垃圾收集如何在JavaScript中工作?

下一篇: JavaScript新的关键字和内存管理