Javascript 中的“继承”是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5027045/
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
What is "inheritance" in Javascript?
提问by Dima
Can anyone explain to me with simple words the meaning of "inheritance" in JavaScript?
谁能用简单的语言向我解释JavaScript中“继承”的含义?
回答by David Tang
In simple terms, inheritance is the concept of one thing gaining the properties or behaviours of something else. To say A inheritsfrom B, is saying that A is a type ofB. A Bird inheritsfrom Animal because a Bird is a type ofAnimal - it can do the same things, but a little more (or differently)!
简单来说,继承是一个事物获得另一事物的属性或行为的概念。说 A继承了 B,就是说 A是B 的一种。鸟继承了动物,因为鸟是动物的一种 - 它可以做同样的事情,但做得更多(或不同)!
In JavaScript, this relationship is a little complicated to specify, but bear with the syntax. You must use a special object called prototypewhich assigns properties to a typesuch as Animal. Only functions have a prototype, which is why you must create a function first:
在 JavaScript 中,指定这种关系有点复杂,但要遵守语法。您必须使用一个名为的特殊对象prototype,该对象将属性分配给诸如 Animal 之类的类型。只有functions 有 a prototype,这就是为什么你必须先创建一个函数:
function Animal() {}; // This is the Animal *Type*
Animal.prototype.eat = function () {
alert("All animals can eat!");
};
Now to create a typethat inherits from Animal, you also use the prototypeobject, but this time the one belonging to the other type, e.g. Bird:
现在要创建一个继承自 Animal的类型,您还可以使用该prototype对象,但这次是属于另一种类型的对象,例如 Bird:
function Bird() {}; // Declaring a Bird *Type*
Bird.prototype = new Animal(); // Birds inherit from Animal
Bird.prototype.fly = function() {
alert("Birds are special, they can fly!");
};
The effect of this is that any Birds you create (called an instanceof Bird) all have the properties of Animals, but they also have the additional .fly():
这样做的结果是,您创建的任何 Birds(称为Bird的实例)都具有 Animals 的属性,但它们还具有额外的.fly():
var aBird = new Bird(); // Create an instance of the Bird Type
aBird.eat(); // It should alert, so the inheritance worked
aBird.fly(); // Important part of inheritance, Bird is also different to Animal
var anAnimal = new Animal(); // Let's check an instance of Animal now
anAnimal.eat(); // Alerts, no problem here
anAnimal.fly(); // Error will occur, since only Birds have fly() in its prototype
回答by sgokhales
A nice explanation by Robert on Inheritance in Javascript here
罗伯特在这里对 Javascript 中的继承的一个很好的解释
The way inheritance works in JavaScript is that it is prototype, instead of class-based.
继承在 JavaScript 中的工作方式是它是prototype,而不是class-based。
For e.g
例如
function Being () {
this.living = true;
}
Being.prototype.breathes = function () {
return true;
An object which inherits Being
一个继承Being的对象
Robert.prototype = new Being;
function Robert () {
this.blogs = true;
}
Robert.prototype.getsBored = function () {
return "You betcha";
};
So, basically, if we create a new instance of the Robert object and call some of its methods, the result would be:
所以,基本上,如果我们创建一个 Robert 对象的新实例并调用它的一些方法,结果将是:
// Create an instance of the Robert object
var me = new Robert();
/*
Returns "You betcha" as it's a method
belonging to the Robert object
*/
me.getsBored();
/*
Returns true. Since the Robert object
doesn't have a breathes method of
its own, it goes back in the
prototype chain to its parent
object, Being, and finds the
method there
*/
me.breathes();
Also a good read of JavaScript inheritance : JavaScript Inheritance
还可以很好地阅读 JavaScript 继承:JavaScript 继承
回答by outis
Robert is right that "inheritance" doesn't mean anything different in JS, but it is implemented differently than in many other languages. To say that an object or class inherits from another means that members (attributes, behavior) can be defined on the parent but accessed through the child. Before you ask what "members" means, objects are aggregate types(aka composite types), and members are the components.
Robert 是对的,“继承”在 JS 中并没有什么不同,但它的实现方式与许多其他语言不同。说一个对象或类继承自另一个意味着成员(属性、行为)可以在父级上定义,但可以通过子级访问。在你问“成员”是什么意思之前,对象是聚合类型(又名复合类型),成员是组件。
JS was originally designed with prototype based inheritance, where parents are objects, but class based inheritance (of a sort) can be shoehorned in by various means. Classes (and thus class inheritance) may be added to later ECMAScriptstandards, which is the standard that defines JS, along with other languages such as ActionScript. ActionScript already supports classes.
JS 最初设计为基于原型的继承,其中父对象是对象,但是基于类的继承(某种)可以通过各种方式硬塞进来。类(以及类继承)可能会被添加到后来的ECMAScript标准中,该标准是定义 JS 的标准,以及其他语言,如 ActionScript。ActionScript 已经支持类。
Closely related to "inheritance" is "extension", where a child extends a parent by adding or overriding members.
与“继承”密切相关的是“扩展”,其中子项通过添加或覆盖成员来扩展父项。
回答by Rizo
Its simple, the inheritance means: "objects/classes inherit from other objects/classes"through prototypes
很简单,继承的意思是:“对象/类通过原型从其他对象/类继承”
for example:
例如:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";
回答by fredrik
It's as simple that you can inherit properties from another javascript object like so:
您可以像这样从另一个 javascript 对象继承属性一样简单:
var a = function () {}
a.prototype.sayHello = function () { alert('hello') }
var b = function () {}
b.prototype = new a();
var c = new b();
c.sayHello(); // Alerts "hello"
John Resig of jQuery has an exellent guide about it http://ejohn.org/apps/learn/about inheritance http://ejohn.org/apps/learn/#76
jQuery 的 John Resig 有一个关于它的优秀指南http://ejohn.org/apps/learn/关于继承http://ejohn.org/apps/learn/#76
EDITUpdated code based on Roberts comment.
编辑基于罗伯茨评论更新的代码。
回答by Robert Koritnik
The meaning of inheritance in JavaScriptis no different than in any object oriented language.
JavaScript中继承的含义与任何面向对象语言中的含义没有什么不同。
Inherited children inherit their parent's behaviour (state possibilities and functionality). These children can of course add additional behaviour or override existing. In this regard inheritance in JavaScript is definitely no different than any other.
继承的孩子继承他们父母的行为(状态可能性和功能)。这些孩子当然可以添加额外的行为或覆盖现有的。在这方面,JavaScript 中的继承绝对与其他继承没有什么不同。
That's about the meaning in as simple words as possible. However you didn't ask how inheritance is implemented in JavaScript because that'd be a completely different story (and question). In that case we should explain differences between classes and prototypes (which are used in JavaScript).
这是关于尽可能简单的词的含义。但是,您没有询问 JavaScript 中如何实现继承,因为那将是一个完全不同的故事(和问题)。在这种情况下,我们应该解释类和原型(在 JavaScript 中使用)之间的区别。

