OOP - Sample of creating Classical Inheritance and Prototypal Inheritance
/* Prototypal Inheritance - Object.create
* Note: using this technique below will not support an older browsers, button
* there is an alternative way of using polyfil!
=======================================================================================================================================*/
// polyfill
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/create
if (!Object.create) {
Object.create = function(o) {
if (arguments.length > 1) {
throw new Error('Object.create implementation' + ' only accepts the first parameter.');
}
function F() {}
F.prototype = o;
return new F();
};
}
var human = {
species: "human",
create: function(name) {
var instance = Object.create(this);
instance.name = name;
return instance;
},
saySpecies: function() {
console.log(this.species);
},
sayName: function() {
console.log(this.name);
}
}
// musican is inherited from human
var musician = Object.create(human);
musician.playInstrument = function() {
console.log("play..." + this.instrument);
}
musician.playInstrument();
musician.saySpecies();
// using Object.create
var will = Object.create(musician)
will.name = "Will";
will.instrument = "Drum";
// using create function
//var will = human.create("will");
will.saySpecies();
will.sayName();
// Classical Inheritance
/* example 1
* Person is a constructor
==========================================================================================*/
function Person(first_name, last_name) {
this.first_name = first_name
this.last_name = last_name
}
Person.prototype.full_name = function() {
return this.first_name + ' ' + this.last_name
}
function Professional(honorific, first_name, last_name) {
Person.call(this, first_name, last_name)
this.honorific = honorific
}
// Add inheritance into pur pseudo classical javascript classic
Professional.prototype = Object.create(Person.prototype)
Professional.prototype.professional_name = function() {
return this.honorific + ' ' + this.first_name + ' ' + this.last_name
}
var professional = new Professional('Dr.', 'Joe', 'Boo');
console.log('===================================================');
console.log(professional.full_name());
console.log(professional.professional_name());
/* example 2
==========================================================================================*/
// Example 2 - using node super_ mothod
function inherits(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
var Person = function(name) {
this.name = name;
}
Person.prototype.sayName = function() {
console.log('Hi my name is ' + this.name);
}
Person.prototype.shoutName = function() {
console.log('Hi my name is ' + this.name + '!');
}
// create instances
var john = new Person("john");
var bobby = new Person("bobby");
//console.log(john.sayName());
//console.log(bobby.shoutName());
var Musician = function(name, instrument) {
// this allowing super_ class or inhetited class constructor
// to set the name and then we're setting the instrument
// then we inherit and then you can go ahead and add your own
// methods
// option 1
//Person.call(this, name);
// option 2 - cleaner way
Musician.super_.call(this, name);
this.instrument = instrument;
}
inherits(Musician, Person); // Musician inherits from Person
Musician.prototype.getInstrument = function() {
console.log(this.instrument);
}
Musician.prototype.shoutName = function() {
console.log("Dude! my name is " + this.name + "!!!");
}
var emma = new Musician("Emma", "Triangle");
console.log(emma)
console.log(emma.sayName())
console.log(emma.getInstrument())
console.log(emma.shoutName())