【问题标题】:Inheritance in Javascript. Do I inherit the methode with the object? How can I reach the result?Javascript中的继承。我是否继承了对象的方法?我怎样才能达到结果?
【发布时间】:2014-04-25 11:21:22
【问题描述】:

我尝试在 Javascript 中行使继承权。 eCar 继承自 car 继承自 Vehicle 的汽车。但似乎我不能将方法“getInfo()”与 Car- 或 eCar-Object 一起使用。 如果我在浏览器中执行此操作,结果是:

Manufacture: Siemens

undefined
undefined

我正在寻找的是:

Manufacture: Siemens
Manufacture: VW
Manufacture: Tesla

.

function Vehicle(herst){

    this.manuf = herst;
}

Vehicle.prototype.getInfo = function(){
    return 'Manufacture: '+ this.manuf+'<br>';
}


Car.prototype = Vehicle;
Car.prototype.construtor = Vehicle;
Car.prototype.getInfo = Vehicle;


function Car(){ }

eCar.prototype = Car;
eCar.prototype.construtor = Car;
eCar.prototype.getInfo = Car;

function eCar(){ }


Train = new Vehicle('Siemens');
document.write(Train.getInfo()+"<br>");


Golf = new Car('VW');
document.write(Golf.getInfo()+"<br>");


Tesla = new eCar('Tesla');
document.write(Tesla.getInfo()+"<br>");

【问题讨论】:

  • 我建议看看Introduction to Object-Oriented JavaScript。它提供了有关如何设置继承的示例。
  • 我认为是Car.prototype = new Vehicle();eCar.prototype = new Car(); 等,不是吗?
  • @Andy 最好不要,您正在创建一个 Vehicle 实例来设置 Car 的原型。 Vehicle 具有实例特定成员,这些成员现在位于 Car 的共享原型上,并且可能会产生意想不到的结果。您可以通过在 Car 构造函数中使用 Vehicle.call(this,args) 来调解这一点,但是当在定义对象时创建 Vehicle 的实例不方便时,您仍然会遇到麻烦。更好地为旧浏览器使用 Object.create 和 polyfil
  • @HMR,这很有趣。感谢您的反馈。

标签: javascript object inheritance methods


【解决方案1】:

你们很亲密,只是有几件事需要不同。

// Vehicle
function Vehicle(herst){
    this.manuf = herst;
}
Vehicle.prototype.getInfo = function () {
    return 'Manufacture: '+ this.manuf+'<br>'; // you really want to return HTML?
};
Vehicle.prototype.construtor = Vehicle;

// Car
function Car(){
    Vehicle.apply(this, arguments); // extends Vehicle
}
Car.prototype = Object.create(Vehicle.prototype); // inherits Vehicle's prototype
Car.prototype.construtor = Car;

// eCar
function eCar(){ // constructors usually start with a capital
    Car.apply(this, arguments); // extends Car
}
eCar.prototype = Object.create(Car.prototype);
eCar.prototype.construtor = eCar;

// use it

var Train = new Vehicle('Siemens'), // variables usually start lower case
    Golf = new Car('VW'),
    Tesla = new eCar('Tesla');

我选择Object.create来设置继承,有些人更喜欢使用Bar.prototype = new Foo()的格式,但我觉得这调用构造函数Foo的时间不对。


这看起来像什么?

var foo = new eCar('Foo');
foo instanceof eCar;    // foo has eCar's prototype
                        // eCar was used to construct foo
foo instanceof Car;     // foo inherited Car's prototype via eCar's prototype
                        // at the beginning of eCar, Car was applied to foo
foo instanceof Vehicle; // foo inherited Vehicle's prototype via Car's prototype
                        // at the beginning of Car, Vehicle was applied to foo
/*
    So `foo` has own properties as assigned by Vehicle, then Car, then eCar,
    and it has the prototype from eCar which shadows the prototype from Car
    which again shadows the prototype from Vehicle
*/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2011-02-20
    • 2018-10-22
    • 2012-03-02
    • 2011-02-04
    • 1970-01-01
    • 2013-07-20
    相关资源
    最近更新 更多