JavaScript prototypes: replace a function

D.R.

I want to decorate a function of a JavaScript "class" (prototype):

SomeClass = function() { this.someVar = 5; };

SomeClass.prototype = {
    foo: function(arg) { /* do something with this.someVar */ }
}

However, I cannot change the source of SomeClass nor am I able to influence the instance creation of SomeClass instances.

Therefore I thought about doing the following:

var oldFoo = SomeClass.prototype.foo;
SomeClass.prototype.foo = function(arg) {
    console.log("Yey, decorating!");
    oldFoo(arg);
};

This seems to work fine, however, due to function scoping oldFoo cannot access someVar anymore (the this object is now window). How to overcome this issue?

Benjamin Gruenbaum

You need to delegate it correctly. What's happening is that since you're calling oldFoo like a bare function the this value is set to undefined (or the global object in non-strict mode).

Apply the method with the arguments and set the this value explicitly:

oldFoo.apply(this, arguments); // use the same `this` and same arguments as called.

Note that in order to be really correct - you also need to return the result. So in total your code should look something like:

SomeClass.prototype.foo = function(arg) {
    console.log("Yey, decorating!");
    return oldFoo.apply(this, arguments); // alternatively capture the return value
};                                        // and process it and then return it

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Creating functions in function prototypes for Javascript

From Dev

JavaScript - The Good Parts: Function prototypes vs Object prototypes

From Dev

React and Javascript: Equivalent for C function prototypes?

From Dev

function prototypes in main function?

From Dev

function prototypes in main function?

From Dev

javascript replace function to replace substring

From Dev

this scope in arrow function prototypes

From Dev

Why use function prototypes?

From Dev

Are there function prototypes in Common Lisp?

From Dev

Function prototypes and argument coercion

From Dev

Why use function prototypes?

From Dev

javascript wildcard replace function

From Dev

Replace function `\` error in the javascript

From Dev

Javascript Regex Function replace()

From Dev

javascript string replace function

From Dev

JavaScript functions and prototypes

From Dev

Understanding of JavaScript Prototypes

From Dev

Javascript nested prototypes

From Dev

Constructor and prototypes in javascript

From Dev

Safely inheriting prototypes in JavaScript

From Dev

Prototypes and property inheritence in JavaScript

From Dev

Prototypes issue in javascript

From Dev

Javascript nested prototypes

From Dev

Safely inheriting prototypes in JavaScript

From Dev

Prototypes and usage Javascript

From Dev

Is there replace function in php? like javascript replace()

From Dev

The best strategy with headers and function prototypes

From Dev

Confused about function declarations and prototypes

From Dev

Are function prototypes needed in header files?