0

What it should do: Calling method on instance should morph "constructor prototype" in different prototype but keeps instance (and all other instances) alive

What I have (written so far):

var differentPrototypeObj = {
    test: function() {
        console.log("it is working");
    }
}

var F = function() {
};

F.prototype = {
    changeMyConstructorPrototype: function() {
        this.constructor.prototype = Object.create(differentPrototypeObj); // doesnt work, but I though it should
        this.constructor.prototype = differentPrototypeObj; // doesnt work, but I though it should
        F.prototype = whatever; // dont want to do this because F is being inherited somewhere and it
    }
};

Testing:

var f = new F();
f.changeMyconstructorPrototype();
console.log(f.test); // want this to be function, but is undefined
console.log(f.changeMyConstructorPrototype); // want this to be undefined, but is still accessible

I guess in my code is this.constructor.prototype but I cant figure out what to use instead.

EDIT - Usage:

It is just concept that comes in my mind. I use it in Angular 1.5 service. Service itself is used to drive form wizard. User can change various things in form and few of them causes large changes across entire form wizard.

This large change must keep instance alive, but changes a lot of behavior (mostly input validations, properties counts and inputs visibility) in both (forward and backward) directions in form wizard.

I create multiple dependent instances and return them from service. Then when I user changes "core" inputs, prototype is changed for the instance parent which does everything else for you.

There could be chosen different approach but I chose this one as experimental and interesting.

Entity Black
  • 3,401
  • 2
  • 23
  • 38
  • I like the formatting, very clear. I would also include a why you want to do this, what are you trying to achieve. Just so we can avoid the XY solution – Callum Linington Jan 07 '16 at 10:43

2 Answers2

0

As I have commented, you cannot change prototype of a function using its instance. Its like changing class structure using its object. This cannot be done.

Even if you try overriding a property in prototype, this will not override it in prototype, but will only add a local property.

var differentPrototypeObj = {
    test: function() {
        console.log("it is working");
    }
}

var F = function() {};

function notify (){
  alert("Hello foo");
}

F.prototype = differentPrototypeObj;

// Additional property
F.prototype.notify = notify;

var f = new F();

f.test();
f.notify();


var f1 = new F();
f1.test = function(){
  console.log("This is new function");
}

f1.test();
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • Thx for fast answer. Usually it would be the solution, but I cant use right now, because method is inherited (as I said), so I dont have access to F. I have only access to context (this). – Entity Black Jan 07 '16 at 10:45
  • You cannot change prototype of a function using its instance. You can add properties to an object but not change it. I have updated my answer. – Rajesh Jan 07 '16 at 10:48
  • You can refer following [post](http://stackoverflow.com/questions/7015693/how-to-set-the-prototype-of-a-javascript-object-that-has-already-been-instantiat) for reference. – Rajesh Jan 07 '16 at 10:53
  • sorry but I can't agree with you. The origin idea I had was to reach constructor function from instance context, which is possible and then change prototype for constructor, which is also possible. But this only affect instances created later and not the existing one. The other solution is in my answer. But thx for your help anyway :) – Entity Black Jan 07 '16 at 21:40
0

Solution I have found works only for one instance and not for all existing instances.

The function is Object.setPrototypeOf() which I used as:

F.prototype = {
    changeMyConstructorPrototype: function() {
        Object.setPrototypeOf(this, Object.create(differentPrototypeObj));
    }
};
Entity Black
  • 3,401
  • 2
  • 23
  • 38