Guest User

Untitled

a guest
Jan 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. function extend(subClass, superClass) {
  2.  
  3. var F = function() {};
  4. F.prototype = superClass.prototype;
  5. subClass.prototype = new F();
  6. subClass.prototype.constructor = subClass;
  7. subClass.superclass = superClass.prototype;
  8. if (superClass.prototype.constructor == Object.prototype.constructor) {
  9. superClass.prototype.constructor = superClass;
  10. }
  11. }
  12.  
  13.  
  14. ================================================================================
  15.  
  16. function Person(name) {
  17. this.name = name;
  18. }
  19. Person.prototype.getName = function() {
  20. return this.name + '-parent';
  21. }
  22.  
  23. function Author(name, books) {
  24. Author.superclass.constructor.call(this, name);
  25. this.books = books;
  26. }
  27.  
  28. extend(Author, Person);
  29.  
  30. Author.prototype.getBooks = function() {
  31. return this.books;
  32. };
  33.  
  34. // overriding parent method
  35. Author.prototype.getName = function(){
  36.  
  37. var authorName = this.name;
  38. var personName = Author.superclass.getName.call(this);
  39.  
  40. console.log('Author name: ', authorName , 'person name' , personName);
  41. };
  42.  
  43. var a = new Author('somename', 'bookname');
  44.  
  45. a.getName();
Add Comment
Please, Sign In to add comment