Advertisement
samimwebdev

javascript object oriented programming

Nov 6th, 2021
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //object architecture
  2. //DefineProperty
  3. // -configurable
  4. // -writable
  5. // - eneumerable
  6. // - get
  7. // - set
  8.  
  9. //prototype chain
  10. 'use strict'
  11.  
  12. // const samim = {
  13. //   firstName: 'samim',
  14. //   lastName: 'Hasan',
  15. //   get age() {
  16. //     return this._age + 'Years old'
  17. //   },
  18. //   set age(age) {
  19. //     if (age < 18) {
  20. //       throw new Error('Minors are not allowed')
  21. //     }
  22. //     this._age = age
  23. //   },
  24. //   fullName() {
  25. //     return this.firstName + ' ' + this.lastName
  26. //   }
  27. // }
  28.  
  29. // Object.defineProperty(samim, '_age', {
  30. //   value: '28',
  31. //   writable: true,
  32. //   enumerable: true,
  33. //   configurable: false
  34. // })
  35.  
  36. //proxy
  37. //constructor
  38.  
  39. //OOJ principle
  40. //encapsulation
  41. //Abstraction
  42. //Inheritance
  43. //polymorphism
  44.  
  45. //creational design pattern (constructor pattern)
  46. function Profile(firstName, lastName) {
  47.   let _age = 28
  48.   this.firstName = firstName
  49.   this.lastName = lastName
  50.   // this.age = age
  51.   Object.defineProperty(this, 'age', {
  52.     get() {
  53.       return _age + 'Years old'
  54.     },
  55.     set(age) {
  56.       if (age < 17) {
  57.         throw new Error('Minors are not allowed')
  58.       }
  59.       _age = age
  60.     }
  61.   })
  62. }
  63.  
  64. Profile.prototype.fullName = function () {
  65.   return this.firstName + ' ' + this.lastName
  66. }
  67.  
  68. function ProfessionalProfile(firstName, lastName, profession) {
  69.   Profile.call(this, firstName, lastName)
  70.   this.profession = profession
  71. }
  72.  
  73. ProfessionalProfile.prototype = Object.create(Profile.prototype)
  74.  
  75. ProfessionalProfile.prototype.professionalName = function () {
  76.   return this.firstName + ' ' + this.lastName + '-' + this.profession
  77. }
  78.  
  79. //setting up lost constructor
  80. ProfessionalProfile.prototype.constructor = ProfessionalProfile
  81.  
  82. const samim = new Profile('samim', 'Hasan')
  83. const samimP = new ProfessionalProfile('samim', 'Hasan', 'web programmer')
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. //constructor to class
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. class Profile {
  108.   #_age = 28
  109.  
  110.   constructor(firstName, lastName) {
  111.     this.firstName = firstName
  112.     this.lastName = lastName
  113.   }
  114.  
  115.   get age() {
  116.     return this.#_age + 'years old'
  117.   }
  118.  
  119.   set age(age) {
  120.     if (age < 18) {
  121.       throw new Error('Minors are not allowed')
  122.     }
  123.     this.#_age = age
  124.   }
  125.  
  126.   fullName() {
  127.     return this.firstName + ' ' + this.lastName
  128.   }
  129. }
  130.  
  131. class ProfessionalProfile extends Profile {
  132.   constructor(firstName, lastName, profession) {
  133.     super(firstName, lastName)
  134.     this.profession = profession
  135.   }
  136.   fullName() {
  137.     return 'Senior Programmer- ' + super.fullName()
  138.   }
  139.   professionalName() {
  140.     return super.fullName() + '-' + this.profession
  141.   }
  142. }
  143.  
  144. const samim = new Profile('samim', 'Hasan')
  145. const samimP = new ProfessionalProfile('samim', 'Hasan', 'web programmer')
  146. const khalil = new Profile('khalil', 'Rahman')
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement