Created
January 22, 2019 03:30
-
-
Save dlucidone/472a1f88f39780cacddd014f969fa7c8 to your computer and use it in GitHub Desktop.
Class Extend Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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