JavaScript 中的重写函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11542192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Override function in JavaScript
提问by user1365697
Possible Duplicate:
Calling base method using JavaScript prototype
可能的重复:
使用 JavaScript 原型调用基本方法
I want to inheritance object that will override function in javascript.
我想继承将覆盖 javascript 中的函数的对象。
From the method I want to call to the base method.
In this case I inherit object reader
from Person
and now I want to override the function getName
meaning that in reader first I want to call the function on Person
and then to do some changes.
从我想调用的方法到基本方法。在这种情况下,我reader
从继承对象Person
,现在我想覆盖该函数,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>
采纳答案by Vincent Robert
Since you are correctly overriding the function on the object itself and not on its prototype, you can still call the prototype function with your object.
由于您正确地覆盖了对象本身而不是其原型上的函数,因此您仍然可以使用您的对象调用原型函数。
reader.getName = function() {
var baseName = Person.prototype.getName.call(this);
...
}
回答by Matt Crinklaw-Vogt
Vincent answered your direct question but here is what you would do if you would like to set up a true inheritance hierarchy where you can further extend Reader
.
Vincent 回答了您的直接问题,但如果您想建立一个真正的继承层次结构,您可以在其中进一步扩展Reader
.
Create your person class:
创建你的个人类:
function Person(name) {
this.name = name;
}
Person.prototype.getName = function(){
alert('Person getName called for ' + this.name);
return this.name;
}
Create a Reader class as well:
还要创建一个 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;
And now we can repeat a similar process to extend Reader with say a VoraciousReader:
现在我们可以重复一个类似的过程,用一个 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.
fiddle: http://jsfiddle.net/7BJNA/1/
小提琴:http: //jsfiddle.net/7BJNA/1/
Object.create: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create
Object.create:https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create
Object.create(arg)
is creating a new object whose prototype is what was passed in as an argument.
Object.create(arg)
正在创建一个新对象,其原型是作为参数传入的。
EditIts been years since this original answer and now Javascript supports the class
keyword which works as you'd expect if you're coming from language like Java or C++. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
编辑这个原始答案已经有好几年了,现在 Javascript 支持class
关键字,如果您来自 Java 或 C++ 等语言,则该关键字可以正常工作。https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
回答by Absolom
I use this technique from John Resig to get inheritance and method overriding. It even lets you access to the overridden method by calling this._super().
我使用 John Resig 的这项技术来获得继承和方法覆盖。它甚至可以让您通过调用 this._super() 来访问被覆盖的方法。
回答by Paddy
This is one way to do it:
这是一种方法:
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());?