Skip to content

Instantly share code, notes, and snippets.

@dlucidone
Created January 22, 2019 03:30
Show Gist options
  • Save dlucidone/472a1f88f39780cacddd014f969fa7c8 to your computer and use it in GitHub Desktop.
Save dlucidone/472a1f88f39780cacddd014f969fa7c8 to your computer and use it in GitHub Desktop.
Class Extend Implementation
function extend(subClass, superClass) {
var F = function() {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superclass = superClass.prototype;
if (superClass.prototype.constructor == Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
}
}
================================================================================
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name + '-parent';
}
function Author(name, books) {
Author.superclass.constructor.call(this, name);
this.books = books;
}
extend(Author, Person);
Author.prototype.getBooks = function() {
return this.books;
};
// overriding parent method
Author.prototype.getName = function(){
var authorName = this.name;
var personName = Author.superclass.getName.call(this);
console.log('Author name: ', authorName , 'person name' , personName);
};
var a = new Author('somename', 'bookname');
a.getName();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment