此代码无法在最后一行运行.我不知道为什么.
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
您的新平面的构造函数具有空函数,并且从未在其上调用Vehicle构造函数.您应该通过更改以下内容从平面构造函数中调用Vehicle构造函数:
var Plane = function() {};
至:
var Plane = function ( ) { Vehicle.call( this ); };