I need to delete the property of Object. Given an "id", I must delete value[id]. I try this code:
delete value[id];
But the delete operator deletes only a reference, never an object itself.Anyone can suggest me any methods to delete forever a objects property?
JavaScript doesn't allow you to do such a thing, it is garbage collected, this means you have no direct control over what happens in memory. You can only delete a reference. Make sure you delete it anywhere else it is used if you want it gone forever.
FROM Mozilla
go through the link, it explains it properly..
Main points from the link is added below..
Unlike what common belief suggests, the delete operator has nothing to do with directly freeing memory (it only does indirectly via breaking references. See the memory management page for more details).
If the delete operator succeeds, it removes the property from the object entirely. However, if a property with the same name exists on the object's prototype chain, the object will inherit that property from the prototype.
delete is only effective on an object's properties. It has no effect on variable or function names.
While sometimes mis-characterized as global variables, assignments that don't specify an object (e.g. x = 5) are actually property assignments on the global object.
delete can't remove certain properties of predefined objects (like Object, Array, Math etc). These are described in ECMAScript 5 and later as non-configurable.
Related
There are other examples, but for the sake of simplicity let's take the length property, what's that doing here:
[].hasOwnProperty("length")
//==> true
As we know, array's length property resides on Array.prototype so it's readily accessible from any array instance just by walking up the prototype chain, why copy it down? Has it something to do with the specific implementation in the browser? (code example above executed in chrome console). Even MDN clearly says that: "...methods and properties are not copied from one object to another in the prototype chain. They are accessed by walking up the chain..."
Simply: because every array instance has a different .length value.
The .length property could have been a getter/setter that would be inherited from a shared prototype object, but the initial design of JavaScript went with a data property that is updated automatically with element creation/removal.
length property resides on Array.prototype
There exists an Array.prototype.length property, but only because Array.prototype is itself an array. Notice that its value is 0 - you wouldn't want that to be inherited to all arrays.
Whenever I open an object details in console.log, after the attached properties or functions I always see "_ proto _". What is this and why is it always present even though I never declared anything like this in my object? I know it is related to some prototype feature but not exactly sure. Also, whereever it exists, whether plain js, jquery, or Angular.js it has the exact same 13 functions below it, viz. defineGetter, defineSetter, ...., set_proto.
Can someone explain it?
So whenever you make any object in JavaScript it inherited from Object. so a object in java script have this structure
private members
proto-Reference of the prototype of current object parent.
prototype- Reference of the current objects prototype
So if you make any object let say named as object1
var object1=new Object();
it will be inherited from Object. and have proto set to Object(you can think Object as root class).
I would like to know the correct way to completely dereference a JavaScript Object from memory. To ensure it's deletion without it dangling in memory, and that the garbage collector removes the object.
When I looked and this question Deleting Objects in JavaScript. It was explained that if you delete all the references of object, the GC will remove it from memory. I want to know how do I go about removing references from an object that has both methods and properties.
Suppose you have and object that was created by using function, and the object has both methods and properties. Say it looks something like this:
function myObject(x, y) {
this.x = x;
this.y = y;
this.myMethod = function() {
// method code
}
}
var myInstance = new myObject(24, 42) // How to remove this completely?
Note: This is an example, I understand you can use prototype for methods.
I know I can't just say delete myInstance. So in this case to completely delete the object will I need to call delete on all it's properties, and then call delete on the instance, like so?
delete myInstance.x;
delete myInstance.y;
delete myInstance; // I'm not sure if this is necessary.
Would this work? Or do I need to also delete it's methods (and if so how)?
Or perhaps there is a better and simpler way to do this?
Javascript is a garbage collected language. It will clean up an object ONLY when there is no other code that has a reference to it. Those other references need to either go out of scope (and not be held by a closure) or you can set those other variables to something else so they don't point at your object. When there are no other variables with a reference to your object, it will be automatically taken care of by the garbage collector, including any properties it has (assuming none of those properties are themselves objects that something has a reference to - but even then the host object would be cleaned up and only the object in the property would continue to live on).
You cannot delete an object any other way in Javascript.
So, to remove the object created by this:
var myInstance = new myObject(24, 42) // How to remove this completely?
Just clear myInstance like this:
myInstance = null;
You don't need to manually delete the properties from myInstance at all. If nobody has a reference to the mother object and none of the properties are objects that someone has a reference to, then the garbage collector will just clean everything up for you.
The delete operator is primarily for removing properties from an object when you want the mother object to remain (e.g. when you just want to remove a property).
I have an object that may or may not have the properties I want to delete; the properties are added at runtime. This is how I'm writing the code:
if (MyObject.hasOwnProperty("SomeProperty")) {
delete MyObject['SomeProperty'];
}
If I remove the condition to test if the property doesn't exist, the code doesn't crash but I'm wondering if this is just because I'm running it in Chrome or if it's valid javascript. In other words, can I write delete MyObject['SomeProperty']; without the .hasOwnProperty statement and be fine even when the object won't have the property.
The conditional here is unnecessary. The delete operation will do nothing if the property doesn't exist on the object. It doesn't remove inherited properties, either.
Delete Documentation
Do I have to destroy instances myself? ...if I don't assign them a variable...will they automatically go away?
new ImageUploadView();
vs
var Iu = ImageUploadView();
If there is no reference to an object in javascript, the garbage collector will clean it up.
The way the garbage collector works is it looks for javascript objects for which nobody has a reference to. If nobody has a reference to it, then it can't be used again, so it can be deleted and the memory it occupied reclaimed. On the other hand, if any javascript entity still has a reference to the object, then it is still "in use" and cannot be deleted.
In your first code example:
new ImageUploadView();
unless the constructor of the object stores away the this pointer into some other variable or object or creates some closure that causes references to the object to be held, then there will be no reference to this new object and it will be cleaned up by the garbage collector.
If you second code example:
var Iu = ImageUploadView();
as long as the Iu variable exists and stays in scope it will contain whatever the ImageUploadView() function returns. Note, the second example, is just executing a function and storing it's value. It is not necessarily creating anything. If ImageUploadView() just returns true, then that's all the Iu variable will contain.
The first method is fine. Assuming that the instance of ImageUploadView is appropriately cleaning up after itself, it will be collected by the garbage collector.
With large objects, it's not necessarily a good practice to assume that the browsers built in garbage collector will clean up once it's out of scope. You're better off to clear it ourself using "delete". For example:
delete MyImageUploadView;
Edit: it may be preferable to set the object to null if it isnt being referenced as a property.