What is the correct way to delete an object that is an argument in a function? I have the following example code :
var theObjects = new Object();
theObjects['first'] = { x:0, y:0 };
theObjects['second'] = { x:0, y:0 };
theObjects['third'] = { x:0, y:0 };
function somefunc(obj){
// that
for(var k in theObjects){
if(theObjects[k] == obj){
delete theObjects[k];
}
}
// or that
delete obj;
}
$(function(){
somefunc(theObjects['first']);
});
My guess is that the first way is right, because I delete the object itself. But on the other hand, objects are passed in a function by reference. So when I delete, do I get rid of the object, or the reference to it?
new Object(), just define your object, ie:theObjects = {}, secondly, you cannot compare two objects like this. They will not == (or ===) each other.==will indeed work.{ x:0, y:0 } == { x:0, y:0 }returns false because they are two separate objects.