Skip to content

Instantly share code, notes, and snippets.

@Williammer
Last active August 29, 2015 14:02
Show Gist options
  • Save Williammer/7cf9a48f94e9c1713232 to your computer and use it in GitHub Desktop.
Save Williammer/7cf9a48f94e9c1713232 to your computer and use it in GitHub Desktop.
jsPatterns.inherit.js -- including the use of prototype, call/apply, constructor to implement the best solution.
function inherit(Child, Parent) {
Child.prototype = new Parent(); //this is the prototype inhert magic!
}
var holyInherit = (function () {
var F = function () {};
return function (C, P) {
F.prototype = P.prototype;
C.prototype = new F();
C.uper = P.prototype;
C.prototype.constructor = C;
};
}());
function Book(name, author) {
this.name = name || 'bkName';
this.author = author || 'bkAuthor';
}
Book.prototype.getName = function () {
return this.name;
};
function Fiction(name) {
this.name = name || 'fiction';
}
inherit(Fiction, Book);
//!!!Better way: use apply for parent properties copy, but not prototype.
function OnlineBook(name, author) {
Book.apply(this, arguments); // copy protpery of Book
//Internet.apply(this, arguments);//can copy mutiple parents at the same time.
}
OnlineBook.prototype = new Book(); // implement prototype inherit.
//proxy function pattern - holy grail inherit pattern.
function Bible(name) {
this.name = name || 'bibName';
}
holyInherit(Bible, Book);
//test
var fic = new Fiction('FutureBokk');
var bk = new Book();
var obk = new OnlineBook();
var bib = new Bible();
var fName = fic.getName();
var fAuthor = fic.author;
var oAuthor = obk.author;
var bName = bib.getName();
var hasAuthor = fic.hasOwnProperty('author');
var bkhsAuthor = bk.hasOwnProperty('author');
var obkhsAuthor = obk.hasOwnProperty('author');
var bibhsAuthor = bib.hasOwnProperty('author');
console.dir(bib);
console.dir(obk);
console.log(bName);
document.body.innerHTML = fName + '--------------' + fAuthor + '<br/>Fiction has own author? ' + hasAuthor + '<br/>Book has own author? ' + bkhsAuthor + '<br/>OnlineBook has own author? ' + obkhsAuthor + '<br/>Bible has own author? ' + bibhsAuthor;
//////////// to organize
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
function Person() {
// an "own" property
this.name = "Adam";
}
// a property added to the prototype
Person.prototype.getName = function () {
return this.name;
};
// create a new person
var papa = new Person();
// inherit
var kid = object(papa);
// test that both the own property
// and the prototype property were inherited
kid.getName(); // "Adam"
// parent constructor
function Person() {
// an "own" property
this.name = "Adam";
}
// a property added to the prototype
Person.prototype.getName = function () {
return this.name;
};
// inherit
var kid = object(Person.prototype);
console.log(typeof kid.getName); // "function", because it was in the prototype
console.log(typeof kid.name); // "undefined", because only the prototype was inherited
//ECMA5 Object.create()
var child = Object.create(parent, {
age: {
value: 2
} // ECMA5 descriptor
});
child.hasOwnProperty("age"); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment