Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Destroying an object reference, then recreating it? not working....

    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.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    When you used

    instanceObj = null;

    how can you say that it doesn't work?

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    well,,,,,

    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...

    instanceObj.attr3

    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.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Can you create a very simple small demo page that illustrates the behavior so that we can get a better picture of it?

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    THIS IS EXACTLY WHAT I NEED:

    var p_00001 = new Array(
    ['153.50',[1,4,1,0],'p_00001'],
    ['100.00',[2,4,7,1],'p_00001'],
    ['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(
    ['153.50',[1,4,1,0],'p_00001'],
    ['100.00',[2,4,7,1],'p_00001'],
    ['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(
    ['253.50',[1,4,1,0],'p_00002'],
    ['200.00',[2,4,7,1],'p_00002'],
    ['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(
    ['353.50',[1,4,1,0],'p_00003'],
    ['300.00',[2,4,7,1],'p_00003'],
    ['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(
    ['353.50',[1,4,1,0],'p_00004'],
    ['999.00',[2,4,7,1],'p_00004'],
    ['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(
    ['small',[16,1,0,0],'size'],
    ['medium',[4,2,7,0 ],'size'],
    ['large',[3,4,7,1 ],'size'],
    ['x-large',[8,8,7,0 ],'size']
    );

    var colors = new Array(
    ['white',[15,14,1,0],'colors'],
    ['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 = [
    p_00001,
    p_00002,
    p_00003,
    p_00004,
    size,
    colors,
    additionals

    ];



    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]);

    alert(myNewObj[0][0])
    return myNewObj;
    }
    Last edited by BrightNail; 06-24-2004 at 12:58 AM.

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    BUMPING THIS THREAD AS I NEED ASSISTANCE BADLY :-)

  • #7
    New Coder
    Join Date
    Nov 2003
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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....

  • #9
    New Coder
    Join Date
    Nov 2003
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why not just create duplicate objects on load as "live" versions then to refresh just copy the values from the "config" data.

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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;

    or

    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.
    Last edited by BrightNail; 06-24-2004 at 01:50 AM.

  • #11
    New Coder
    Join Date
    Nov 2003
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Start with two objects with duplicate configs, different names.

    CarObjectLive
    CarObjectData

    var liveobj = CarObjectLive();

    on refresh...

    var defaultobj = CarObjectData();

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

    ?????????????

  • #12
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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();

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    There's a similar version (it's called copy) of that which is posted in this thread.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •