3

If I want to set the prototype I need to do this always outside of the function/object. I want to do that with this.prototype...

example:

MyPrototype = function() {
    this.name = "MyPrototype";
    this.number = 3;
}
MyObjectToExtend = function() {
    this.prototype = new MyPrototype(); //<-- I want this
    this.name = "MyObjectToExtend";
}
o = new MyObjectToExtend();

If i want to get o.number i will get nothing. But it is possible to access it with o.prototype.numer but I don't think that this is how you should get it, because the bellow example is working fine...

MyPrototype = function() {
    this.name = "MyPrototype";
    this.number = 3;
}
MyObjectToExtend = function() {
    this.name = "MyObjectToExtend";
}
MyObjectToExtend.prototype = new MyPrototype(); //<-- I don't want this
o = new MyObjectToExtend();

Now i can access o.number and it will give me 3 as it should be.

But I don't want this, I want to set the prototype as in the top example...

TheEquah
  • 121
  • 8
  • 6
    The problem with what you want is that it doesn't work. What you don't want does work. Javascript is a rollercoaster ride of emotions like that, forcing you to write code that works – Jaromanda X Oct 06 '15 at 08:38
  • 1
    You might find the answer here: http://stackoverflow.com/a/9516739/115493 – mik01aj Oct 06 '15 at 08:39
  • @JaromandaX It is probably so with `prototype`. I only want to "share" properties and functions with `MyObjectToExtend` without adding tonnes of code outside of the "construction/definition" (I'm not an expert, hopefully this is called right) where i can extend them... – TheEquah Oct 06 '15 at 08:49
  • I'm not quite sure why you want to do this. Before ES2015, this is not possible AFAIK (in ES2015 you have the syntactic sugar of "classes"). Just a sidenote. You probably don't want to call the constructor just to use an object as a prototype link. You can try the following instead: `MyObjectToExtend.prototype = object.create(MyPrototype.prototype);` – nils Oct 06 '15 at 09:16
  • 3
    Hi- this reads like a statement of frustration rather than a question - could you explain what you're asking ? – user2808054 Oct 06 '15 at 09:16
  • @TheEquah: If you don't like the code structure, have a look [at this answer](http://stackoverflow.com/a/28256166/1048572). Really, you must not place code in the constructor that should not be re-executed for every instance. That's just how it works. – Bergi Nov 10 '15 at 23:54

1 Answers1

0

In this case, the this keyword is a reference to an instance of your class. There is a property called __proto__ that allows you to change the value of the prototype of this particular instance, but its use is not recommended and it won't affect other instances of your class in this situation.

You can use Object.getPrototypeOf(this).number = 3 to add a property to your prototype, but you won't be able to change the value of your prototype without using MyObjectToExtend.prototype = new MyPrototype(); or __proto__.

Rodrigo5244
  • 5,145
  • 2
  • 25
  • 36