2

This code cannot run on the last line. I am not sure why.

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
yes4me
  • 159
  • 3
  • 15

2 Answers2

6

Your new plane has an empty function for its constructor and the Vehicle constructor has never been called on it. You should call the Vehicle constructor from the plane constructor, by changing:

var Plane = function() {};

to:

var Plane = function ( ) {
    Vehicle.call( this );
};
Paul
  • 139,544
  • 27
  • 275
  • 264
1

Extending the object could be done with the below

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

var Plane = function(){
    Vehicle.apply(this, arguments);
};
Plane.prototype = Object.create(Vehicle.prototype);

var plane = new Plane();
console.log( plane instanceof Vehicle , plane.getAge());

jsFiddle

Ross The Boss
  • 624
  • 5
  • 17