Skip to content

Instantly share code, notes, and snippets.

@shyamsalimkumar
Created July 4, 2014 18:08
Show Gist options
  • Save shyamsalimkumar/ca5debbf2d9791c0d1ca to your computer and use it in GitHub Desktop.
Save shyamsalimkumar/ca5debbf2d9791c0d1ca to your computer and use it in GitHub Desktop.
JavaScript Inheritance models
// Declare the constructor
var Human = function ( name ) {
this._name = name;
};
// Method to get the human's name
Human.prototype.getName = function () {
return this._name;
};
// Creating a new human (creating a new instance, instantiation, of Human)
var man = new Human( 'James Howlett' );
man.getName(); // James Howlett
// The child constructor calls the parent constructor
var Mutant = function ( ) {
this._codeName = arguments[1]; // Don't wanna override the constructor? Use the next line only
Human.apply( this, arguments );
};
// Birds and bees yo
Mutant.prototype = Object.create(Human.prototype);
Mutant.prototype.constructor = Mutant;
//
// I usually use Mutant.prototype = new Human(); instead
//
// Overriding part of the inherited `getName` function
Mutant.prototype.getName = function () {
return Human.prototype.getName.call( this ) + ' - ' + this._codeName;
};
// Creating a new mutant (creating a new instance, instantiation, of Mutant)
var wolverine = new Mutant( 'James Howlett', 'Wolverine' );
wolverine.getName(); // James Howlett - Wolverine
// Futuristic shit!!! - ECMAScript6
class Human {
constructor ( name ) {
this._name = name;
}
getName () {
return this._name;
}
}
// Creating a new human (creating a new instance, instantiation, of Human)
var man = new Human( 'James Howlett' );
man.getName(); // James Howlett
// Triplets must be tough
class Mutant extends Human {
// super() calls the parent method
constructor ( name, codeName ) {
this._codeName = codeName;
super( name );
}
// Overriding part of the inherited `getName` function
get () {
return super() + this._codeName;
}
}
// Creating a new mutant (creating a new instance, instantiation, of Mutant)
var wolverine = new Mutant( 'James Howlett', 'Wolverine' );
wolverine.getName(); // James Howlett - Wolverine
var Human = {
constructor: function ( name ) {
this._name = name;
},
getName: function () {
return this._name;
}
};
// Don't try
// var man = new Human( 'James Howlett' );
// specifically new Human( 'James Howlett' );
// You'll get `TypeError: object is not a function`
// Cause Human is not a function :) it's an object
// Creating a new human (creating a new instance, instantiation, of Human)
var man = Object.create(Human);
man.constructor( 'James Howlett' );
man.getName(); // James Howlett
// Yeah, its a boy!!!
var Mutant = Object.create(Human);
// Don't write a constructor function if you don't wanna override the parent constructor
Mutant.constructor = function () {
this._codeName = arguments[1];
Human.constructor.apply(this, arguments);
};
// Overriding part of the inherited `getName` function
Mutant.getName = function () {
return Human.getName.call(this) + ' - ' + this._codeName;
};
// Creating a new mutant (creating a new instance, instantiation, of Mutant)
var wolverine = Object.create( Mutant );
wolverine.constructor( 'James Howlett', 'Wolverine' )
wolverine.getName(); // James Howlett - Wolverine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment