Advertisement
rajuahammad73

04. Javascript OOP(Class) Lesson

Mar 14th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function User(name, job, birthYear) {
  2.     this.name = name;
  3.     this.job = job;
  4.     this.birthYear = birthYear;
  5.     this.getFullName = function(){
  6.         return this.name;
  7.     }
  8. }
  9.  
  10. User.prototype.calcAge = function() {
  11.     return 2020 - this.birthYear;
  12. };
  13.  
  14. let john = new User("John", "dev", 1995);
  15. // console.dir(User)
  16. console.log(john)
  17.  
  18. // const User2 = {
  19. //     name: 'John'
  20. // }
  21.  
  22. // console.log(User2);
  23.  
  24.  
  25. function SubUser(name, job, birthYear, salary, isMarried){
  26.     User.call(this, name, job, birthYear);
  27.     this.salary = salary;
  28.     this.isMarried = isMarried;
  29. }
  30. SubUser.prototype = Object.create(User.prototype)
  31.  
  32. var dave = new SubUser('dave', 'dev', 1975, 500000, true);
  33. console.log(dave.calcAge());
  34.  
  35.  
  36. class UserClass {
  37.     constructor(name, job, birthYear){
  38.         this.name = name;
  39.         this.job = job;
  40.         this.birthYear = birthYear;
  41.     }
  42.  
  43.     static getFullName(){
  44.         return this.name;
  45.     }
  46.  
  47.     calcAge(){
  48.         return 2020 - this.birthYear;
  49.     }
  50. }
  51.  
  52. let jane = new UserClass('Jane', 'teacher', '1990')
  53. // console.log(jane)
  54.  
  55. class UserSubClass extends UserClass {
  56.     constructor(name, job, birthYear, salary, isMarried){
  57.         super(name, job, birthYear);
  58.         this.salary = salary;
  59.         this.isMarried = isMarried;
  60.     }
  61. }
  62.  
  63. let mike = new UserSubClass('Mike', 'designer', '1988', 40000, true);
  64. console.log(mike);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement