0

I have a class that I want to extend with a couple of new methods.
In order not to pollute the object's namespace (possible overwrite of existing methods) more than necessary I want to group these methods under one property name.
How can I get a reference for the original object's instance inside of the extended container?

To clarify:

// here's what I have
obj = function () {
    // something happens here
    this.property = "XYZ"
}
var x = new obj();
console.log(x.property); // logs "XYZ"

// here's how I want to extend it
obj.prototype.extension = {property: "ABC"};
obj.prototype.extension.whatstheproperty = function () {
    // how to get a reference to the obj's instance here?
    console.log(this.property); // logs "ABC" not "XYZ" for obvious reasons
    // QUESTION -> how get get "XYZ"?
}
var z = new obj();
z.extension.whatstheproperty();

My guess is there must be a way to store a reference to the parent instance as a property of extension.
An obvious way would be to just set it whenever the parent is instanced:

x = new obj();
x.extension.parent = x;

Then I could access this.parent inside all methods of extension.

But I feel there must be a better way, especially because I don't know when a new obj is instanced.

I just want every new obj to have a property extension that has a property referencing the parent instance.

How should I approach this?

Jan Paepke
  • 1,997
  • 15
  • 25
  • No. The reason why it's no is because prototypes are shared, so as soon as you make a new instance you'd lose the previous `obj`. You'd either need to put `extension` on the instance and not the prototype, or put `whatstheproperty` on the instance, or invoke it with `call`/`apply`. – Paul S. Dec 09 '14 at 15:39
  • well both can only be done after the obj is instanced. You're saying there is no general solution? – Jan Paepke Dec 09 '14 at 15:41
  • Do you have access to the original constructor? I mean, can you modify the constructor? – Leo Dec 09 '14 at 15:42
  • Yous could use `obj.extension.whatstheproperty.call(obj)` – Oriol Dec 09 '14 at 15:43
  • @Leo I could yes, but I wouldn't know about the specific name 'extension' at that point, if that's why u're asking. – Jan Paepke Dec 09 '14 at 15:43
  • 1
    I'm starting to think it's probably best to just prefix my extension methods like `obj.prototype.extension_whatstheproperty`... – Jan Paepke Dec 09 '14 at 15:45
  • @JanPaepke why don't you create a sub class? As you pick the phrase "extension" that implies you are extending something... – Leo Dec 09 '14 at 15:49
  • @Leo So you would do something like `x.extension = new extension(x);` and store the reference in the constructor? Interesting idea... I'll think about it... – Jan Paepke Dec 09 '14 at 15:54
  • what i meant was, you could define sub class like `var Ext = function(){}; Ext.prototype = new obj();`, then you can define extension methods like `Ext.prototype.method = function(){...}`. After that instantiate with `new Ext` – Leo Dec 09 '14 at 16:14
  • Oh, forgot to add `Ext.prototype.constructor = Ext`, which fixes the constructor for sub class. Generally traditional OOP style js. – Leo Dec 09 '14 at 16:17

0 Answers0