0
/**
 * adds an entry to the list
 * 
 * @param data {Object}
 * @return this
 */
ElementList.prototype.addEntry = function(data){
    if (!data) return this;

    data['type'] = this.children_type;

    // add the entry to the current elements
    this.element_list.push(new FavoriteEntry(data));

    this.refresh();

    return this;
};

/**
 * favorites extend ElementList
 *
 * @param setting_list
 * @constructor
 */
function FavoriteList(setting_list){
    ElementList.call(this, setting_list);
}

FavoriteList.prototype = new ElementList();
FavoriteList.constructor = FavoriteList;

So this a short code snipplet of an educational project of mine. What I want to do is reduce repeating code so I created a generic ElementList object So

  • the Example FavoriteList inherties the parent Objects prototype
  • The constructors is pointing to the Childobject
  • the Parent constructor is called within the child.

That works just perfectly fine my problem is

 // add the entry to the current elements
this.element_list.push(new FavoriteEntry(data));

This should create a new instance of an Object BASED on the CHILD so therefore I need to get the name of the child instance that's calling the parent method

i tried - this.constructor (point to the parent) - this.constructor.name - this instanceof FavoriteList (works)

since I DON'T want to pass a name and i think iterating through instanceof "options" is not really smart.

I would ask for some insights how I can access the childs instance name in the parent elements method body.

Please I only need an explicit answer to this!! I already read workarounds! If It's not possible just say so :)

thx in advance :)

shadowdroid
  • 91
  • 2
  • 5
  • thought so :/ thx, so an ugly solution it is – shadowdroid Jan 14 '14 at 14:00
  • I'm confused: So you do something like this: `var favList = new FavoriteList("xyz"); favList.addEntry("some data");` and you want to access `favList` inside of `addEntry`? – basilikum Jan 14 '14 at 14:11
  • well the data is an object not a string -> it creates element in predefined html container. but since the events should be handled different base on the parent container i need to create dynamic instances of the "entries" the FavList should contain FavEntries so I want to generate the "FavEntries" call -> "var call = FavList.constructor.replace(/List/, '')" + "Entry"(); something like that. – shadowdroid Jan 14 '14 at 14:37
  • var call = this.constructor.name.replace('List','') + "Entry"; window[call](data); that's what i wanted to do to be excact :) – shadowdroid Jan 14 '14 at 14:43

3 Answers3

1
this.element_list.push(new FavoriteEntry(data));

This should create a new instance of an Object BASED on the CHILD so therefore I need to get the name of the child instance that's calling the parent method

No, you don't seem to need to know the name. All you need is a helper function to generate new Entry instances, that can be overwritten to generate more specific entries. Maybe you're already doing that by passing a children_type with the data

i tried - this.constructor (point to the parent)

It should work if you had set the constructor correctly. Change your code to

FavoriteList.prototype.constructor = FavoriteList;
//          ^^^^^^^^^^

Also, you might want to use Object.create instead of new to set up the prototype chain.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • ahh okay that made sense :) works like a charm now. i already change it to the ecma standard Object.create I just didn't want to repost the code :) thx for the input :) – shadowdroid Jan 14 '14 at 14:32
1

I'm not sure if I fully understand but the code new FaforiteEntry should create either a FororiteEntry or another type based on the current object type.

Maybe the following example could help you out:

var ElementList = function(args) {
  this.element_list = [];
}
ElementList.prototype.addEntry = function(args) {
  this.element_list.push(new this.entryType(args.val));
};
//will create element_list items of type String
ElementList.prototype.entryType = String;

function FavoriteList(args) {
  ElementList.call(this, args);
}
FavoriteList.prototype = Object.create(ElementList.prototype);
FavoriteList.constructor = FavoriteList;
//will create element_list items of type Array
FavoriteList.prototype.entryType = Array;

//adding entries to f would create items of type Array
var f = new FavoriteList();
f.addEntry({val: 2});
console.log(f.element_list);//[[undefined, undefined]]
//adding entries to e would create items of type String
var e = new ElementList();
e.addEntry({val: 2});
console.log(e.element_list);//[ String { 0="2"...
HMR
  • 37,593
  • 24
  • 91
  • 160
  • thx but Bergi gave me the answer I needed. i had to assign the obj.prototype.constructor so I could access the name of the child object that called the generic object. :) `var call = this.constructor.name.replace('List','') + "Entry"; // add the entry to the current elements this.element_list[data.id] = new window[call](data);` that's what I wanted to do :) – shadowdroid Jan 14 '14 at 15:55
  • 1
    @shadowdroid I see, please note that the variable FavoriteList is assigned to a nameless function in my code and this.name would not return what you expect. – HMR Jan 15 '14 at 00:01
1

Simple code example:

 function Parent(){
        // custom properties
    }

    Parent.prototype.getInstanceName = function(){
        for (var instance in window){
            if (window[instance] === this){
                return instance;
            }
        }
    };

    var child = new Parent();

    console.log(child.getInstanceName()); // outputs: "child"
shmuli
  • 5,086
  • 4
  • 32
  • 64