View Full Version : Destroying an object reference, then recreating it? not working....

06-23-2004, 07:11 AM
I'm not at work, so I don't have the code handy...

but basically, I create an object with some properties....during some dropdown interataction everything is working great. I'm manipulating the object attributes and such.....

Okay, I have a reset button. When the user selects that --- I basically want to destroy or "reset" all the attributes to the 'start' position...so to speak.

Well, when I do it...it doesn't work.....Here is something similiar I'm doing..

function someObj(){
this.attr1 = blah blah
this.attr2 = ..
this. attr3 = new Array;

var instanceObj = new someObj;

function resetit(){

instanceObj = null; //didn't work
delete instanceObj; //didn't work
var instanceObj2 = new someObj;
var instanceObj = instanceObj2; // didn't work
var instanceObj = new someObj; // didn't work

basically....I want the reset link to search as a page reset in which all objects/attributes and such are reset to their "originaL" place...

what am I missing? Basicallly, during my interactions with the dropdowns...I refer to attributes like so: "instanceObj.attr2" etc......I figure if I destroy 'instanceObj"...then I cleared everything.....but it doens't work........perhaps my object references are wrong.?

any ideas.

06-23-2004, 07:19 AM
When you used

instanceObj = null;

how can you say that it doesn't work?

06-23-2004, 07:44 AM

I have a data structure...and I take a snapshot of it when I create that object...and I reference the data via an array...


well, basically - I search thru this array, and delete any entries that don't match the searchfunction I do...so eventually, I am left with a smaller version of the array..great, works fine...

but when I try to delete:
instanceObj = null;

and I Hit reset ---> basically, the dropdowns are suppose to repopulate with the 'full' array data structure...basically, a pristine array object without the omissions from the searchfunction I created....well, it doesn't work..the reset just resets the dropdowns with the "current" version of : instanceObj.attr3.

its almost as if the object is persistent.

06-23-2004, 08:04 AM
Can you create a very simple small demo page that illustrates the behavior so that we can get a better picture of it?

06-23-2004, 07:40 PM

var p_00001 = new Array(
['80.50' ,[4,2,7,0],'p_00001'],
['70.00' ,[8,8,7,0],'p_00001'],
['60.00' ,[16,1,0,0],'p_00001']

var p_00001 = new Array(
['80.50' ,[4,2,7,0],'p_00001'],
['70.00' ,[8,8,7,0],'p_00001'],
['60.00' ,[16,1,0,0],'p_00001']
var p_00002 = new Array(
['88.50' ,[4,2,7,0],'p_00002'],
['77.00' ,[8,8,7,0],'p_00002'],
['66.00' ,[16,1,0,0],'p_00002']
var p_00003= new Array(
['88.80' ,[4,2,7,0],'p_00003'],
['77.70' ,[8,8,7,0],'p_00003'],
['66.60' ,[16,1,0,0],'p_00003']
var p_00004 = new Array(
['999.00' ,[4,2,7,0],'p_00004'],
['0.00' ,[8,8,7,0],'p_00004'],
['0.00' ,[16,1,0,0],'p_00004']
var size = new Array(
['medium',[4,2,7,0 ],'size'],
['large',[3,4,7,1 ],'size'],
['x-large',[8,8,7,0 ],'size']

var colors = new Array(
['black', [14,14,2,1],'colors'],
['blue', [14,14,4,0],'colors']
var additionals = new Array(
['long sleeve',[2,4,2,1],'additional']

var CartData = [


WHAT I NEED TO DO IS: CLONE CartData....apparently, when I assign that to my object properties, and I manipulate the object var -- it actually manipulates the ORIGINAL ARRAYS....... cause the object is a pointer to the originals...

I NEED to clone CartData exactly so that I can reassign it back to the object VAR intact on form reset...

please advise....I found this code, but doens't seem to work.

var cloneData = clone (CartData);

function clone(myObj)

if(typeof(myObj) != 'object') return myObj;
if(myObj == null) return myObj;

var myNewObj = new Object();

for(var i in myObj)
myNewObj[i] = clone(myObj[i]);

return myNewObj;

06-24-2004, 12:59 AM

06-24-2004, 01:23 AM
Maybe I am to simple, but since Javascript is pretty much a single load langauge, why not just reload the page, then all the info will be back to default. If you need to retain some items, just submit them to itself with a flag for which items you wish to retain. Since the naming will be the same it would be easy to code an updater that retains the state of the partialy changed fields.

06-24-2004, 01:28 AM
I can't 'refresh' page cause it doesn't work with the needed functionality...

basically, I 'could' do that --- but the idea is to have a 'refresh' button, they click that and .......basically, this is where I'm having issue...cause the object won't refresh because I've manipulated the arrays......this is why I need to 'clone' them so I can assign the full values back into the object var...etc....

06-24-2004, 01:40 AM
Why not just create duplicate objects on load as "live" versions then to refresh just copy the values from the "config" data.

06-24-2004, 01:44 AM
that is the problem....

you can't duplicate...you can create instances, but unless it is a clone, then you are always modifying the original data via instances....

if it was as simple as duplicating...I'd just do something like..

var copy1 = new CartObject();
var copy2 = copy1;


var copy1 = new CartObject();
var copy2 = new CartObject();

and then just manipulate copy2, and then when needed reassign copy1 back to it...but it doesn't work like that. I could be wrong, perhaps I'm overlooking something easy here.

06-24-2004, 01:58 AM
Start with two objects with duplicate configs, different names.


var liveobj = CarObjectLive();

on refresh...

var defaultobj = CarObjectData();

liveobj.....element....= defaultobj.....element


06-24-2004, 02:30 AM
right..I see what you are saying.....I don't want to duplicate the configs cause they can be rather large on the return from the app....

this seems to work:

Array.prototype.clone = function () {
var tmp = [];
for (i in this) {
if (this[i].constructor == Array) {
tmp[i] = this[i].clone();
} else {
tmp[i] = this[i];
return tmp;
var copyD = CartData.clone();

06-24-2004, 03:41 AM
There's a similar version (it's called copy) of that which is posted in this thread (http://www.codingforums.com/showthread.php?t=19996).

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum