Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

object in prototype is inherited as reference

I want to inherit new object instance using prototype.

Test case:

var MyObj = function() {}
MyObj.prototype.objName = {} 
// I want this to be a different object for each instance of MyObj

var o1 = new MyObj (),
    o2 = new MyObj ();

o1.objName['a'] = 1;
o2.objName['a'] = 2;

alert(o1.objName['a']) // 2
alert(o1.objName === o2.objName) // true

This means that objects in prototype are not inherited as its copies but instead as its reference.

I know that normally you can do it like this.

var MyObj = function() {
    this.objName = {}
}

var o1 = new MyObj(),
    o2 = new MyObj();

alert(o1.objName === o2.objName) // false

This works fine, but in my case this is not an option. I really need to define objName outside the MyObj function.

I managed to "solve" the problem with this

MyObj.prototype.objName = function() {
    if ( this._objName === undefined ) {
        this._objName = {};
    }
    return this._objName;
}

var o1 = new MyObj(),
    o2 = new MyObj();

o1.objName()['a'] = 1;
o2.objName()['a'] = 2;

alert(o1.objName()['a']) // 1

But this is not very pretty and the performance of this code is much worse.

Is there any way to solve this more elegantly ?

like image 524
Filip Minx Avatar asked Jan 18 '26 21:01

Filip Minx


1 Answers

This means that objects in prototype are not inherited as its copies but instead as its reference.

Nothing on the prototype is copied - the whole concept of prototypical inheritance is that properties reference the shared properties of the prototype object. So if you want a property to be individual for each instance, you have to explicitly assign it to the object and shadow the prototype property; just as you're doing it with the _objName property in your code.

But this is not very pretty and the performance of this code is much worse.

If you want it pretty, move it to the constructor (or make the constructor look for something like an init method to call if exists, then you can create that init method on the prototype.

To make performance a little better, you can change the getter function to

MyObj.prototype.getObj = function() {
    var obj = {};
    this.getObj = function(){ return obj; }; // overwrite itself
    return obj;
};

though it still has the function call overhead. For even more elegance, you can use a getter property (not supported in old browsers) that removes itself on the first access:

Object.defineProperty(MyObj.prototype, "objName", {
    get: function() {
        var obj = {};
        Object.defineProperty(this, "objName", {
            value: obj,
            writable: true //?
        });
        return obj;
    },
    enumerable: true,
    configurable: true
});

Now you can omit the function call parenthesis.

like image 90
Bergi Avatar answered Jan 20 '26 10:01

Bergi