你如何调用从另一个类继承的方法?

yes*_*4me 2 javascript

此代码无法在最后一行运行.我不知道为什么.

var Vehicle = function() {
	var age = 21;			//private variable
	this.setAge = function(age2) {age = age2;};
	this.getAge = function() {return age;};
};

var Plane = function() {};
Plane.prototype = Object.create(Vehicle.prototype);

var plane = new Plane();
console.log( plane instanceof Vehicle );
//console.log( plane.getAge() );	//TypeError: plane.getAge is not a function
Run Code Online (Sandbox Code Playgroud)

Pau*_*aul 6

您的新平面的构造函数具有空函数,并且从未在其上调用Vehicle构造函数.您应该通过更改以下内容从平面构造函数中调用Vehicle构造函数:

var Plane = function() {};
Run Code Online (Sandbox Code Playgroud)

至:

var Plane = function ( ) {
    Vehicle.call( this );
};
Run Code Online (Sandbox Code Playgroud)