I have a class like this:
function Foo() {
this._current = -1;
}
Foo.prototype.history = {};
Foo.prototype.history.back = function () {
if (this._current === undefined) {
return alert("this._current is undefined");
}
--this._current; // `this` is the history object
};
How can I access the Foo
instance in the back
method?
I solution I see is to do something like this:
var f = new Foo();
f.history.back = f.history.back.bind(f);
Is there a better solution? Doing that for every Foo
instance does not sound good for me.
Here is an example:
function Foo() {
this._current = -1;
}
Foo.prototype.history = {};
Foo.prototype.history.back = function() {
if (this._current === undefined) {
return alert("this._current is undefined");
}
--this._current; // `this` is the history object
};
var f = new Foo();
f.history.back();
I know it's supposed to be so, but what's the correct way to solve this type of problem?