Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中的子类无法继承在类外声明的方法_Javascript_Inheritance_Constructor_Prototype - Fatal编程技术网

javascript中的子类无法继承在类外声明的方法

javascript中的子类无法继承在类外声明的方法,javascript,inheritance,constructor,prototype,Javascript,Inheritance,Constructor,Prototype,这些代码行没有意义: // declared class vehicle function Vehicle(make,model,year){ this.make=make this.model=model this.year=year } // 2 - Add a function to the Vehicle prototype called start which returns the string //"VROOM!" Vehicle.prototype.start=f

这些代码行没有意义:

// declared class vehicle
function Vehicle(make,model,year){
  this.make=make
  this.model=model
  this.year=year

}
// 2 - Add a function to the Vehicle prototype called start which returns the string //"VROOM!"
 Vehicle.prototype.start=function(){
  return "VROOM!"
}

// declared child class car
function Car(make,model,year){
  Vehicle.apply(this,arguments)
  this.numWheels=4

}
// assigning the constructor
Car.prototype.constructor=Object.create(Vehicle.constructor)
// changing the pointing of constructor back to car
Car.prototype.constructor=Car;
var sedan=new Car("Tractor", "John Deere", 1999)
console.log(sedan.start()) 
//sedan.start() gives error but if i declare it inside vehicle class does not 
它们没有意义,因为向该构造函数赋值,然后立即进行另一个赋值,意味着第一个赋值对任何东西都绝对没有影响

我认为你需要的是:

// assigning the constructor
Car.prototype.constructor=Object.create(Vehicle.constructor)
// changing the pointing of constructor back to car
Car.prototype.constructor=Car;
现在,您将拥有
汽车
原型指的是
汽车
原型,这样
汽车
实例将在其原型链中同时拥有这两个原型

在这个2020年的现代世界中,您可以使用来实现所有这些,以获得
Car()
来继承
Vehicle()
原型上定义的方法,您只需使用:

// assigning the constructor
Car.prototype=Object.create(Vehicle.prototype)
// changing the pointing of constructor back to car
Car.prototype.constructor=Car;
这里的方法用于创建一个新对象,并使其成为
Car.prototype
的值。新对象的原型为
Vehicle.prototype
,因此将继承
Vehicle.prototype
上可用的所有方法。因此,不需要使用以下逻辑:

Car.prototype = Object.create(Vehicle.prototype);
演示:

//申报类别车辆
功能车(品牌、型号、年份){
这个
this.model=model
今年
}
//2-向车辆原型添加名为start的函数,该函数返回字符串//“VROOM!”
Vehicle.prototype.start=函数(){
返回“VROOM!”
}
//申报儿童级汽车
多功能车(品牌、型号、年份){
车辆。应用(此,参数)
这个.numWheels=4
}
Car.prototype=Object.create(Vehicle.prototype);
Car.prototype.constructor=Car;
var轿车=新车(“拖拉机”,“约翰迪尔”,1999年)

console.log(sedan.start())
您尚未将
汽车
的原型设置为
汽车
。非常感谢!!在我的课程中,我直到现在才开始上课。谢谢你的回答。难道我不该把汽车的构造师。原型机换成汽车吗。从我所知道的,如果我没有改变它将指向车辆constructorOk,但我无法得到你为什么需要它。如果您不将
Car.prototype
的构造函数再次更改为
Car
,您是否可以分享一个测试用例场景,其中您遇到了问题。testcase-->因为假设我添加了一个类似Car.bonet()的方法如果我没有显式地将其更改为vechicle构造函数,那么它也会将自身添加到vechicle的构造函数中至少这是我认为的好,那么我们还需要将Car.prototype的构造函数再次更改为Car.thnks,以供您回复
// assigning the constructor
Car.prototype.constructor=Object.create(Vehicle.constructor)