0

Here is the sample code that I am trying to execute.

var Game = function(){
  this.state = 'yeah';
}

Game.prototype.call = function(){
  document.writeln(this.state);
  Game.prototype.say();
}

Game.prototype.say = function(){
  document.writeln(this.state);
}

game = new Game();
game.call();

The result is yeah undefined which means that the call() is working properly while the say() isn't. What can I do for say() function to be able to get this.state from Game object?

2
  • 1
    I would not recommend defining a method .call(). While it technically may work in some circumstances, it could really confuse people with Function.call() which every function has already. Commented Apr 22, 2013 at 4:45
  • You shouldn't be using writeln as if the document is closed (e.g. after the load event has been dispatched), it will first call document.open which will clear the document of existing content. Commented Apr 22, 2013 at 5:01

4 Answers 4

2
Game.prototype.call = function(){
  document.writeln(this.state);
  this.say();
}

prototype is used in for defining the function - not calling it

Sign up to request clarification or add additional context in comments.

Comments

0

never, please never override native methods (like call in this case)..

also something like this works too

Game.prototype.call = function(){
  document.writeln(this.state);
  Game.prototype.say.apply(this);
}

1 Comment

call is not a native function on objects, it exists on Function.prototype, but doesn't get hidden in this case. I agree that you should avoid naming functions call, but it's not that big of a deal.
0

It looks like what you want is:

Game.prototype.call = function(){
  document.writeln(this.state);
  this.say();
}

However this version will call whatever function is set at this.say, which might be overridden if the object is inherited:

var MyGame = function () {};
MyGame.prototype = new Game();
MyGame.prototype.say = function () {
    document.writeln('something else');
};
var m = new MyGame();
m.call(); //'something else'

If you want to use the original reference to Game.prototype.say (without inheritance), then you'll need to call the function in the context of the object:

Game.prototype.call = function(){
  document.writeln(this.state);
  Game.prototype.say.call(this);
}
var m = new MyGame();
m.call(); //'yeah'

Comments

0

TGH has given you a solution but not explained it. Your problem is here:

> Game.prototype.say();

You are calling say as a method of Game.prototype, so in the function:

> Game.prototype.say = function(){
>   document.writeln(this.state); 
> }

this is a reference to the prototype object, not the instance. You want to call the function as:

 this.say();

so that it's called as a method of the instance, thereby setting this within say to the instance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.