Javascript中的继承。我是否继承对象的方法?如何获得结果?

天空

我尝试在Javascript中行使继承性。从汽车继承的eCar从汽车继承的汽车。但似乎我无法将方法“ 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>");
保罗·S。

您很亲密,只需要几件事就可以有所不同。

// 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
*/

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从Doctrine2单表继承存储库中获得混合对象类型结果集?

来自分类Dev

JavaScript中的对象继承

来自分类Dev

如何获得想要的继承结果?

来自分类Dev

如何从JSON继承JavaScript对象?

来自分类Dev

javascript对象继承如何工作?

来自分类Dev

如何使 nhibernate 不合并继承对象的结果?

来自分类Dev

面向对象,javascript中的继承无法调用外部方法

来自分类Dev

面向对象,javascript中的继承无法调用外部方法

来自分类Dev

Javascript原型与对象中的对象的继承

来自分类Dev

Java:如何创建从继承的类中获取对象的getter方法?

来自分类Dev

Java:如何创建从继承的类中获取对象的getter方法?

来自分类Dev

R参考类-如何确定您是否在继承的方法中?

来自分类Dev

Javascript:使对象继承数组方法

来自分类Dev

JavaScript中的面向对象的继承

来自分类Dev

JavaScript中的面向对象的继承

来自分类Dev

在 Javascript 中扩展(继承)类(或对象)

来自分类Dev

如何防止在JavaScript中执行继承属性的getter setter方法

来自分类Dev

如何在Screeps对象中实现继承?

来自分类Dev

如何从继承访问对象

来自分类Dev

如何从继承访问对象

来自分类Dev

Swift继承问题。如何获得适当的类对象

来自分类Dev

Typescript 中的对象继承

来自分类Dev

我如何继承DataGridColumns

来自分类Dev

我如何继承DataGridColumns

来自分类Dev

用JavaScript中的方法进行类继承

来自分类Dev

JavaScript中的继承:调用父级的方法

来自分类Dev

如何创建一个新对象继承 JavaScript 中的现有对象?

来自分类Dev

JavaScript检查对象是否具有继承的属性

来自分类Dev

JPA:TypedQuery无法获得继承类型的结果

Related 相关文章

热门标签

归档