JavaScript中的重写函数

JavaScript中的重写函数,javascript,prototype,Javascript,Prototype,可能重复: 我想创建一个继承对象,它将覆盖javascript中的函数 从我要调用的方法到基方法。 在本例中,我从Person继承了objectreader,现在我想覆盖函数getName,这意味着在reader中,我首先要调用Person上的函数,然后做一些更改 <script> /* Class Person. */ function Person(name) { this.name = name; } Person.protot

可能重复:

我想创建一个继承对象,它将覆盖javascript中的函数

从我要调用的方法到基方法。 在本例中,我从
Person
继承了object
reader
,现在我想覆盖函数
getName
,这意味着在reader中,我首先要调用
Person
上的函数,然后做一些更改

<script>
    /* Class Person. */
    function Person(name) {
        this.name = name;
    }
    Person.prototype.getName = function() {
        return this.name;
    }

    var reader = new Person('John Smith');
    reader.getName = function() {
        // call to base function of Person, is it possible?
        return('Hello reader');
    }
    alert(reader.getName());
</script>

/*班上人*/
职能人员(姓名){
this.name=名称;
}
Person.prototype.getName=函数(){
返回此.name;
}
var reader=新人(“约翰·史密斯”);
reader.getName=函数(){
//调用Person的基本函数,是否可能?
return('Hello reader');
}
警报(reader.getName());

由于您正确地覆盖了对象本身而不是其原型上的函数,因此仍然可以使用对象调用原型函数

reader.getName = function() {
    var baseName = Person.prototype.getName.call(this);
    ...
}

这是一种方法:

function Person(name) {
this.name = name;
}
Person.prototype.getName = function(){
return this.name;
}

var reader = new Person('John Smith');
reader.oldGetName = reader.getName;
reader.getName = function() {
//call to base function of Person , is it possible ?
    return this.oldGetName();
}
alert(reader.getName());​

我使用John Resig的这项技术来获得继承和方法重写。它甚至允许您通过调用此来访问重写的方法。\ u super()


Vincent回答了您的直接问题,但如果您想建立一个真正的继承层次结构,在这里您可以进一步扩展
Reader

创建您的person类:

function Person(name) {
    this.name = name;
}

Person.prototype.getName = function(){
    alert('Person getName called for ' + this.name);
    return this.name;
}
同时创建一个Reader类:

function Reader(name) {
    // Calls the person constructor with `this` as its context
    Person.call(this, name);
}

// Make our prototype from Person.prototype so we inherit Person's methods
Reader.prototype = Object.create(Person.prototype);

// Override Persons's getName
Reader.prototype.getName = function() {
    alert('READER getName called for ' + this.name);
    // Call the original version of getName that we overrode.
    Person.prototype.getName.call(this);
    return 'Something';
}
Reader.prototype.constructor = Reader;
现在我们可以重复一个类似的过程来扩展读者,比如说贪婪的读者:

function VoraciousReader(name) {
    // Call the Reader constructor which will then call the Person constructor
    Reader.call(this, name);
}

// Inherit Reader's methods (which will also inherit Person's methods)
VoraciousReader.prototype = Object.create(Reader.prototype);
VoraciousReader.prototype.constructor = VoraciousReader;
 // define our own methods for VoraciousReader
//VoraciousReader.prototype.someMethod = ... etc.
小提琴:

Object.create:

Object.create(arg)
正在创建一个新对象,其原型是作为参数传入的对象

编辑
它已经有多年的原始答案,现在JavaScript支持<代码>类< /Cord>关键字,它可以像你所期望的那样从java或C++语言中工作。p> Vincent的答案似乎比这要简洁得多-可能是更好的方法…为了更动态地使用它,请使用
Object.getPrototypeOf(this.getName.call)(this)如果ES5可用。我会使用Person.prototype.getName.apply(这是参数),这样就不会丢失任何参数。除非您特别不想传递参数。这应该是选择的答案,这很清楚,并准确地显示了javascript中的虚拟继承是如何使用原型实现的,这在许多其他答案中是模糊的