Ionică Bizău
Ionică Bizău

Reputation: 113475

Accesing `this` value when the prototype contains an object?

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?

Upvotes: 1

Views: 70

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382464

The fundamental problem in your code is that you have only one history object shared between all instances of Foo. You must make it one history per instance. A solution would be this:

function FooHistory(foo){
	this._foo = foo;
}
FooHistory.prototype.back = function() {
  if (this._foo._current === undefined) {
    return alert("this._foo._current is undefined");
  }
  this._foo._current--; 
};

function Foo() {
	this._current = -1;
	this.history = new FooHistory(this);
}
var f = new Foo();
f.history.back();

(you probably want to have _current in the FooHistory instance instead of the Foo instance, I tried to change as little code as possible)

Note that other solutions are possible, depending on the larger view. If you don't have any state to store in the history object then you could also have Foo.prototype.history() return an object with a property linking back to the Foo instance. Then you would call f.history().back().

Upvotes: 5

Related Questions