覆盖原型方法

Overiding a prototype method

本文关键字:方法 原型 覆盖      更新时间:2023-09-26

我有一个这样的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函数的警报。

它对我来说很好用。(它提醒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()