Nikita Gavrilov
Nikita Gavrilov

Reputation: 547

How to change function inside constructor in JavaScript?

I need to edit the function which locates inside of the constructor. Example:

some.thing = function() {
    this.somefn = function() { // this is the function that I need to fix
        ...
    }
}

But function should be edited not just only for a single object (new obj = some.thing();) but also for any created objects by this constructor.

So is there any way to edit such inner-functions?

Upvotes: 0

Views: 834

Answers (3)

gbs
gbs

Reputation: 3529

Here is a solution based on prototype:

var Something = function () {
    this.f = function () {
       console.log("Something");
    };    
};
var Old = Something;
var Something = function () {
    Old.apply(this);
    this.f = function () {
        console.log("New");
    };
};
Something.prototype = new Old();

var s = new Something();
s.f(); // prints "New"

Upvotes: 2

I Hate Lazy
I Hate Lazy

Reputation: 48761

The solutions seem just a little too obvious, so I'm wondering if the trouble is that you don't have access to the original code, and you need a more dynamic solution.

If so, one option may be to override the constructor with your own constructor, and have it call the original, and then update the object.


Original code:

some.thing = function() {
    this.somefn = function() { // this is the function that I need to fix
        ...
    }
}

Your code:

       // cache a reference to the original constructor
var _thing = some.thing;

               // your constructor
some.thing = function() {

             // invoke the original constructor on the new object.
    _thing.apply(this, arguments);

    this.somefn = function() { /*your updated function*/ };
};

        // maintain inheritance
some.thing.prototype = Object.create(some.thing.prototype);

  // make an instance
var theThing = new some.thing();

Now you're getting the benefit of the original constructor and prototype chain, but you're injecting your own function on to the objects being created.

Only trouble may be that the original function you replaced could make special use of the original constructor's variable scope. If that's the case, there would be an issue to resolve.

It would be possible to retain and invoke the original method that you overwrote before invoking yours. Not sure if this situation calls for that or not.

Upvotes: 2

Rikki
Rikki

Reputation: 3528

I exactly know your need cause last week I passed through it. I just implemented a complete inheritance model in javascript and as far as I remember, I had a problem with overriding constructors and calling the parent class's ctor when child class is initializing.

So I just solved the problem with modifing some points in my design and it's now working like a charm! (something like C# but in Javascript)

By the way, I don't suggest you to change a method contents this way, but here is a way to do that (I myself did not do that this way and AGIAIN I DO NOT RECOMMEND IT. THERE ARE MANY OTHER WAYS, BUT THIS IS THE EASIEST):

var test = function() { /*InjectionPlace*/ };

eval("var newTest = " + test.toString().replace(
     "/*InjectionPlace*/", 
     "var i = 10; alert(i);"
));

test();

newTest();

Cheers

Upvotes: 0

Related Questions