覆盖原型方法

时间:2013-11-14 22:15:04

标签: javascript

我有像这样的javascript基本功能;

roomBase = function () {
  this.go = function(){
    alert(1);
  }
}

我有这样的房间对象;

myRoom = function(){
  this.go = function(){
    alert(456);
  }
}
myRoom.prototype = new roomBase();
theRoom = new myRoom();

当我致电theRoom.go()时,我收到来自prototype的提醒。我想要的是来自myRoom功能的警报。

1 个答案:

答案 0 :(得分:2)

对我来说很好。 (它警告456

您确定一切正常吗?

演示:http://jsfiddle.net/9hWAr/

演示代码:

var roomBase = function () {
  this.go = function(){
    alert(1);
  }
}

var myRoom = function(){
  this.go = function(){
    alert(456);
  }
}
myRoom.prototype = new roomBase();
var theRoom = new myRoom();

theRoom.go()