2014-08-28 82 views
2

我试图使用继承,但是我被卡住了。我收到一个错误,不知道我做错了什么。我在网上看到的所有示例都不会将对象传递到构造函数中,所以我不确定我应该做什么。这里是一个简单化的例子 -JavaScript - 继承错误

function Automobile(obj){ 
    this.name = obj.name; 
    this.model = obj.model; 
} 

Automobile.prototype = { 
    getName: function(){ 
     console.log(this.name); 
    }, 
    getModel: function(){ 
     console.log(this.model); 
    } 
} 

var bmw = new Automobile({ 
    name: 'BMW', 
    model: 'm5' 
}) 

bmw.getName(); 
bmw.getModel(); 


function Truck(obj){ 
    this.cabSize = obj.cabSize 
} 

Truck.prototype = new Automobile(); 
Truck.prototype = { 
    getCabSize: function(){ 
     console.log(this.cabSize); 
    } 
} 

var fordF150 = new Truck({ 
    name: 'Ford', 
    model: 'F150' 
}) 

//Uncaught TypeError: Cannot read property 'name' of undefined test.js:2 
//Automobile test.js:2 
//(anonymous function) 
+0

当你调用'Truck.prototype = new Automobile();你期望第一个参数是一个对象时,你还没有传递一个对象。 – Musa 2014-08-28 23:16:15

回答

3

错误发生在Truck.prototype = new Automobile();。你没有传递obj,这是Automobile期望的。

为了避免这种尝试Truck.prototype = Object.create(Automobile.prototype)

在使用

Truck.prototype.getCabSite = function(){ 
} 

后,这样你就不会从汽车覆盖继承的属性。

+0

有没有什么办法可以做到这一点,并保持Truck.prototype = {...}添加新的方法?没有什么大不了的,我只是喜欢用这种方式添加原型。 – Ben 2014-08-28 23:25:49

+0

你可以做'卡车。prototype.WhateverYouWantToAdd = function(){}'一遍又一遍 – user145400 2014-08-28 23:27:33

0

取而代之的是:

Truck.prototype = { 
    getCabSize: function(){ 
     console.log(this.cabSize); 
    } 
} 

试试这个:

Truck.prototype.getCabSite = function(){ 


} 

Truck.prototype.constructor = Truck; //this will insure the appropriate constructor is called 

这样,你不删除摆脱汽车protoype的

+0

谢谢onzur。我以某种方式想要保持Truck.prototype = {}格式,所以我可以以这种方式添加更多的方法。不知道如何设置卡车继承汽车,仍然这样做?无论如何,我仍然会收到“Uncaught TypeError:无法读取未定义的属性'名称'错误与此更新。 – Ben 2014-08-28 23:16:37

+0

如果你使用jQuery,你可以使用'.extend()'来合并对象。 – Barmar 2014-08-28 23:17:47

0
  1. 您更改了卡车的“原型” - >您无法继承。

    Truck.prototype = new Automobile();
    Truck.prototype = { getCabSize:function(){ console.log(this.cabSize); } }; //更改卡车

    的原型

    可以修复

    Truck.prototype.getCabSize =函数(){};

1关于原型的变化的例子,我写的一些代码行中使用您的汽车功能

function Automobile(obj){ 
    this.name = obj.name; 
    this.model = obj.model; 
} 

Automobile.prototype = { 
    getName: function(){ 
     console.log(this.name); 
    }, 
    getModel: function(){ 
     console.log(this.model); 
    } 
} 

var bmw = new Automobile({ 
    name: 'BMW', 
    model: 'm5' 
}) 

Automobile.prototype = { 
      sayHello:function(){ 
      console.log("Hello, I'm new"); 
     } 
} 

// Make new Automobile 

var honda = new Automobile({ 
    name: 'BMW', 
    model: 'm5' 
}); 

// Try to call getName, getModel 
bmw.getName(); // BMW 
bmw.getModel(); // m5 

honda.getName(); // TypeError : undefined is not function 
honda.getModel(); // TypeError : undefined is not function 

// Try to call sayHello 

bmw.sayHello(); // TypeError : undefined is not function 

honda.sayHello(); // Hello, I'm new 

宝马为什么不能打电话的sayHello和本田不能调用的getName和getModel? 因为我在创建bmw对象后改变了汽车的原型参考。 bmw保存对包含getName,getModel而不是sayHello的“旧”原型的引用。本田持有含有sayHello但不包含getName和getModel的“新”原型的引用。

希望它能帮助你。