1. #1
    Touch Premium Member
    Join Date
    Apr 2007
    Posts
    139
    Vote Rating
    0
    mherger is on a distinguished road

      0  

    Default How to instantiate an object of the same class as "this"?

    How to instantiate an object of the same class as "this"?


    I'm currently playing with a tree which has nodes of different type (all inherited from AsyncTreeNode). They are different in what they are loading, deciding what's a leaf etc. I'd therefore like to have a node's child be of the same "origin", instantiated from the same class as the current node, inheriting even most of its parameters. How can this best be done? Right now I'm copying the options object to the object itself, to be reused when a child is created. Something like

    Code:
    SlimTreeNode = function(options){
    	SlimTreeNode.superclass.constructor.call(this);
    	this.options = options;
    }
    This can't be the best way :-)

    Regards,

    Michael

  2. #2
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,505
    Vote Rating
    53
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    You are not creating anything there, just calling the superclass's constructor. A bit like

    Code:
        super();
    in Java.

    To instantiate a SlimTreeNode, use "new SlimTreeNode()"!

  3. #3
    Touch Premium Member
    Join Date
    Apr 2007
    Posts
    139
    Vote Rating
    0
    mherger is on a distinguished road

      0  

    Default


    thanks for the pointer. Yep, my example code was badly chosen: it only shows how I'm carrying around the options to be used later in something like this in my SlimTreeLoader:

    Code:
    createNode : function(attr, node){
       return new SlimTreeNode(node.options);
    }
    But then I'm not sure whether this is an issue or not. I guess the object is only carrying references to the options around?

    Michael

  4. #4
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,505
    Vote Rating
    53
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    You're creating a new node with the same options as an existing one. Looks right to me.

    But what's the "attr" param?

  5. #5
    Touch Premium Member
    Join Date
    Apr 2007
    Posts
    139
    Vote Rating
    0
    mherger is on a distinguished road

      0  

    Default


    So you don't think this is a clunky solution? Great :-)

    attr (not used in the simplified example) are the future child node's attributes, while node is the current node, to be expanded.

    I'm using a customized processResponse in my SlimTreeLoader which passes attr and node to createNode.

    Thanks again, I'll then leave this as is.

    Michael

Thread Participants: 1