我在读一本关于JavaScript中继承的教程,并有下面的语句:Javascript继承与原型 - 冗余对象
兔类的一个对象从动物类继承,我们需要:
- 从动物定义动物
- 定义兔
继承兔子:
Rabbit.prototype =新动物()
他们说这种方法的缺点是需要创建一个冗余对象。我不明白为什么我需要创建多余的对象?我已经尝试了下面的方法,它没有创建多余的对象:
function Animal() {};
function Rabbit() {};
Rabbit.prototype = Animal.prototype
Animal.prototype.go = function() {alert("I'm inherited method"};
var r = new Rabbit();
r.go();
我在这里错过了什么?
+1指出'Object.create' –