【发布时间】:2011-06-28 23:05:37
【问题描述】:
谁能用简单的话给我解释一下JavaScript中“inheritance”的含义?
【问题讨论】:
谁能用简单的话给我解释一下JavaScript中“inheritance”的含义?
【问题讨论】:
很简单,继承的意思是:“对象/类从其他对象/类继承”通过原型
例如:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";
【讨论】:
Robert 对 Javascript here 中的继承的一个很好的解释
JavaScript 中继承的工作方式是prototype,而不是class-based。
例如
function Being () {
this.living = true;
}
Being.prototype.breathes = function () {
return true;
继承存在的对象
Robert.prototype = new Being;
function Robert () {
this.blogs = true;
}
Robert.prototype.getsBored = function () {
return "You betcha";
};
所以,基本上,如果我们创建一个 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();
也是 JavaScript 继承的好读物:JavaScript Inheritance
【讨论】:
JavaScript 中的继承 的含义与任何面向对象的语言没有什么不同。
继承的子级继承其父级的行为(状态可能性和功能)。这些孩子当然可以添加额外的行为或覆盖现有的。在这方面,JavaScript 中的继承绝对与其他继承没有什么不同。
这就是用尽可能简单的词来表达的意思。但是,您没有询问 JavaScript 中如何实现继承,因为那将是一个完全不同的故事(和问题)。在这种情况下,我们应该解释类和原型(在 JavaScript 中使用)之间的区别。
【讨论】:
很简单,您可以像这样从另一个 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"
jQuery 的 John Resig 有一个关于它的优秀指南 http://ejohn.org/apps/learn/ 关于继承http://ejohn.org/apps/learn/#76
编辑根据罗伯茨的评论更新了代码。
【讨论】:
b.prototype = a.prototype 之后,你不能在b 的原型中添加任何东西而不影响a。
b.prototype = new a();?否则b 实际上不是从a 继承的,而是它的别名/同义词。
简单来说,继承是一个事物获得其他事物的属性或行为的概念。说 A 从 B 继承,就是说 A 是 B 的一种类型。鸟 继承 动物,因为鸟 是一种动物 - 它可以做同样的事情,但更多(或不同)!
在 JavaScript 中,这种关系的指定有点复杂,但请注意语法。您必须使用名为prototype 的特殊对象,它将属性分配给type,例如Animal。只有functions 有prototype,这就是为什么你必须先创建一个函数:
function Animal() {}; // This is the Animal *Type*
Animal.prototype.eat = function () {
alert("All animals can eat!");
};
现在要创建一个继承自 Animal 的 type,您还需要使用 prototype 对象,但这次是属于另一个 type 的对象,例如鸟:
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!");
};
这样做的效果是您创建的任何鸟(称为鸟的实例)都具有动物的属性,但它们还具有额外的.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
【讨论】:
Robert 说得对,“继承”在 JS 中并不意味着任何不同,但它的实现方式与许多其他语言不同。说一个对象或类继承自另一个意味着成员(属性、行为)可以在父对象上定义,但可以通过子对象访问。在你问“成员”是什么意思之前,对象是aggregate types(又名composite types),成员是组件。
JS 最初是用prototype based inheritance 设计的,其中父对象是对象,但可以通过各种方式硬塞入基于类的继承(某种意义上)。类(以及因此类继承)可以添加到以后的ECMAScript 标准,这是定义 JS 的标准,以及其他语言,如 ActionScript。 ActionScript 已经支持类。
与“继承”密切相关的是“扩展”,其中子通过添加或覆盖成员来扩展父。
【讨论】: