Advertisement
sashomaga

Classical OOP

Jun 1st, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2. // Classical OOP
  3.  
  4. // Inherit
  5. Function.prototype.inherit = function (parent) {
  6.     this.prototype = new parent();
  7.     this.prototype.constructor = parent;
  8. }
  9.  
  10. // Base class
  11. function Human(fname, lname, age)
  12. {
  13.     this.fname = fname;
  14.     this.lname = lname;
  15.     this.age = age;
  16. }
  17.  
  18. // Student -> Human
  19. function Student(fname, lname, age, grade) {
  20.     Human.call(this, fname, lname, age);
  21.     this.grade = grade;
  22. }
  23.  
  24. Student.inherit(Human);
  25.  
  26. Student.prototype.introduce = function () {
  27.     return "Hello! My name is " + this.fname + " " + this.lname + ", my age is: " + this.age + " grade: " + this.grade;
  28. }
  29.  
  30. // Teacher -> Human
  31. function Teacher(fname, lname, age, speciality) {
  32.     Human.call(this, fname, lname, age);
  33.     this.speciality = speciality;  
  34. }
  35.  
  36. Teacher.inherit(Human);
  37.  
  38. Teacher.prototype.introduce = function () {
  39.     return "Hello! My name is " + this.fname + " " + this.lname + ", my age is: " + this.age + " my speciality is: " + this.speciality;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement