38

重複の可能性:
JavaScriptプロトタイプを使用してベースメソッドを呼び出す

javascriptの関数をオーバーライドする継承オブジェクトが欲しいです。

メソッドからベースメソッドを呼び出します。この場合、オブジェクトを継承readerします。Person次に、関数をオーバーライドします。getNameつまり、リーダーで最初に関数を呼び出して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>
4

4 に答える 4

37

Vincentはあなたの直接の質問に答えましたが、さらに拡張できる真の継承階層を設定したい場合は、次のようにしますReader

個人クラスを作成します。

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;

そして今、私たちは同様のプロセスを繰り返して、たとえばVoraciousReaderで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.

フィドル: http: //jsfiddle.net/7BJNA/1/

Object.create:https ://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create

Object.create(arg)プロトタイプが引数として渡されたものである新しいオブジェクトを作成しています。

編集 この最初の答えから何年も経ちましたが、Javascriptは、classJavaやC++などの言語から来ている場合に期待どおりに機能するキーワードをサポートしています。https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

于 2012-07-18T13:45:32.783 に答える
34

プロトタイプではなくオブジェクト自体で関数を正しくオーバーライドしているので、オブジェクトを使用してプロトタイプ関数を呼び出すことができます。

reader.getName = function() {
    var baseName = Person.prototype.getName.call(this);
    ...
}
于 2012-07-18T13:13:32.427 に答える
5

John Resigのこの手法を使用して、継承とメソッドのオーバーライドを取得します。this._super()を呼び出すことで、オーバーライドされたメソッドにアクセスすることもできます。

http://ejohn.org/blog/simple-javascript-inheritance/

于 2012-07-18T13:16:05.467 に答える
2

これはそれを行う1つの方法です:

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());​

http://jsfiddle.net/fXWfh/

于 2012-07-18T13:14:33.477 に答える