tags:

views:

24

answers:

1

I have an on object like so

var obj = {};
function set()
{
   obj.x1 = 20;
   obj.y1 = 35;
   obj.x2 = 60;
   obj.y2 = 55;
   ...
}

Whats the quickest way to delete/reset all of the properties of obj?

+3  A: 
for (p in obj) {
    if (obj.hasOwnProperty(p)) {
        delete obj[p];
    }
}

If you only have one reference to the object, then replacing it with a new one would be faster.

obj = {};
David Dorward
See the second thing is what I kept trying but it didn't work. I'll try this answer and hopefully it will
Shaunwithanau
Although it turned out I was resetting the wrong thing, this is a very useful thing to know. Thanks for the help
Shaunwithanau
Yeah, obj = {} should work. I was trying to answer with that but it wouldn't let me, but see you updated your answer with that anyways.
Shawn Steward