0

All examples and questions I've came upon searching on the web about prototypal inheritance shows the assigning of prototypes to constructor functions and before it's called, much like the following code:

Object.beget = function (o) {
    var F = function () {};
    F.prototype = o;
    return new F();
};

My question is, is there a way to change an object (not a constructor function) prototype after it has been instatiated in such a way that I can access the new prototype methods without calling it directly, or in other words, in such a way where the object inherits its new prototype methods?

EDIT:

I guess the focus of the question might not be clear, I am not interested in enhancing an object's prototype, what I really want is a way to assign a new prototype, without altering other objects which had the same prototype as the first.

4
  • instead of trying to hijack a prototype for a single instance, just create a new object instance. Commented Apr 4, 2015 at 18:51
  • @zzzzBov That would work, but I don't want to pass all values the previous object had to the new one. Commented Apr 4, 2015 at 18:58
  • That's why it's common to see an extend method. jQuery has one, and so does underscore. Commented Apr 4, 2015 at 19:44
  • possible duplicate of How to set the prototype of a JavaScript object that has already been instantiated? Commented Apr 4, 2015 at 19:51

3 Answers 3

1

Is there a way to change an object's prototype after it has been instatiated?

Yes, there is: Object.setPrototypeOf (ES6 only), the counterpart of Object.getPrototypeOf - which access an object's real prototype, not just the .prototype property. There is also the .__proto__ getter/setter property (deprecated) that does the same (see Quick Javascript inheritance: Understanding __proto__).

However, please notice that it is usually a terrible idea to do that. Not only because there may be engines that don't support these, but it defeats all the fancy optimisations an engine uses of instances: Why is mutating the [[prototype]] of an object bad for performance?.

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I needed, thanks alot for your answer and references @Bergi
0

You can assign new properties to your original object, o, and the new object instance will automatically have access to those properties:

Object.beget = function (o) {
    var F = function () {};
    F.prototype = o;
    return new F();
};

var o = {
    a: 1
};

var instance = Object.beget(o);

instance.a === o.a; // true

o.b = function () {};

instance.b === o.b; // true

1 Comment

Actually, my doubt is the other way around, I dont want to enhance an previous prototype, I would like to change an object prototype entirely without changing the prototype of other objects which had the same prototype.
0

If the prototype associated with the object changes after creation of object (which means all the properties that have come from previous prototype chain should be removed and new properties are added), isn't it equal to creating a instance of new object with this new prototype? In which case, why not create a new object and use it instead?

Please let me know if I am missing any use case.

1 Comment

As I mentioned in another commentary, the solution of creating a new Object would work, but it's not the same as 'hijacking' the object's prototype, as all values of the previous object attributes would have to be transfered to the new one, besides the overhead of creating a new instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.