这是我试图执行的示例代码。Javascript嵌套的原型方法范围
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();
结果是yeah undefined
这意味着call()
是否正常工作,而say()
不是。我能做些什么say()
函数能够从Game对象获取this.state?
我不会推荐定义一个'.call()'方法。尽管它在技术上可能在某些情况下起作用,但它可能会让人们用每个函数已有的Function.call()来混淆。 – jfriend00 2013-04-22 04:45:35
您不应该使用['writeln'](http://www.w3.org/TR/2000/WD-DOM-Level-2-HTML-20001113/html.html#ID-35318390)文档被关闭(例如,在加载事件被调度之后),它将首先调用['document.open'](http://www.w3.org/TR/DOM-Level-2-HTML/html.html# ID-72161170),它将清除现有内容的文档。 – RobG 2013-04-22 05:01:02