class Inheritance - Node.js Object

Node.js examples for Object:Class Definition

Description

class Inheritance

Demo Code


/**//from w w  w .  jav a2 s  .  co m
 * Inheritance
 */

function Truck(){
  this.doors = 2;
  this.bed = true;
}

Truck.prototype.go = function(){
  console.log('vroom');
};

var genericTruck = new Truck();

console.log('genericTruck: ', genericTruck);
console.log('genericTruck.ram: ', genericTruck.ram);

function Dodge(){
  this.ram = true;
  // this.doors = 4;
}

Dodge.prototype = new Truck();

var dodgeTuck = new Dodge();
console.log(dodgeTuck);

console.log(dodgeTuck.ram);
console.log(dodgeTuck.doors);
dodgeTuck.go();

function Dakota(config){
  this.capacity = config.capacity;
  this.color = config.color || 'black';
  this.ram = true;
  // this.doors = 4;
}

Dakota.prototype = new Dodge();

var dakota = new Dakota({capacity: '1/2 ton', color: 'white'});

console.log(dakota.capacity);
console.log(dakota.color);

Related Tutorials