Must be phrasing my question wrong, since I cannot find existing questions on what would seem an often issue.
I have a class Foo
.
var Foo = function () {
return {
a: function () {}
};
};
I have another class Bar
that is instantiated with an instance of Foo
:
var Bar = function (foo) {
// Does something with foo.
};
I want to extend the Foo
instance to work differently for Bar
:
var Bar = function (foo) {
foo.a = function () {
return foo.a.apply(this, arguments);
};
// Does something with foo.
};
Doing the way I did it, will cause RangeError: Maximum call stack size exceeded
. Is there a way to extend the method itself without a temporary variable?
This is the way to do it using an extra variable:
var Bar = function (foo) {
var originala;
originala = foo.a;
foo.a = function () {
return originala.apply(this, arguments);
};
// Does something with foo.
};