Skip to content

Instantly share code, notes, and snippets.

@rbrahul
Created September 3, 2017 12:39
Show Gist options
  • Save rbrahul/69a7a11cf575617aee3c27b885506edf to your computer and use it in GitHub Desktop.
Save rbrahul/69a7a11cf575617aee3c27b885506edf to your computer and use it in GitHub Desktop.
Inheritance using Object Literal (Object Linked with Other Object ) - OLOO
Object.prototype.oloo = function(o1, o2){
if( o1 && o2 && typeof o1 === "object" && typeof o2 === "object"){
// create new object
var o0 = Object.create(o1);
// copy all props to the brand new obj
for( var key in o2 ){
if(o2.hasOwnProperty(key)){
o0[key] = o2[key];
}
}
// set parent reference
o0.parent = o1;
return o0;
}
return null;
}
var Object1 = {
name: null,
init: function(name){
this.name = name;
console.log(this.name+" - main");
}
};
var Object2 = Object.oloo( Object1, {
middleName: null,
init: function(name, middelName){
this.parent.init(name);
this.middelName = middelName;
console.log(this.middelName+" - 1");
}
});
var Object3 = Object.oloo( Object2, {
lastname: null,
init: function(name, middleName, lastname){
this.parent.init(name, middleName);
this.lastname = lastname;
console.log( this.lastname +" - 2");
},
getFullName: function(){
return this.name +" "+ this.middelName +" "+ this.lastname;
}
});
Object3.init("Daniel", "Teles", "Meneses");
console.log( Object3.getFullName() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment