7.属性的封装.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. (function () {
  2. class Person {
  3. constructor(name1, age1) {
  4. this.name1 = name1;
  5. this.age1 = age1;
  6. }
  7. /**
  8. * get 获取
  9. * set 设置
  10. */
  11. get aa() {
  12. return this.name1;
  13. }
  14. set bb(val) {
  15. this.name1 = val;
  16. }
  17. }
  18. let p = new Person('孙悟空', 20);
  19. console.log(p);
  20. // Person.name1 = '你好'
  21. // p.name1 = 'haha';
  22. console.log(p, '111');
  23. // p.aa()
  24. console.log(p.aa);
  25. p.bb = '大家好';
  26. console.log(p);
  27. // p.getName();
  28. // console.log(Person)
  29. class A {
  30. constructor(num) {
  31. this.num = num;
  32. }
  33. get num1() {
  34. return this.num;
  35. }
  36. set num1(val) {
  37. this.num = val;
  38. }
  39. }
  40. class B extends A {
  41. say() {
  42. console.log(this.num);
  43. }
  44. }
  45. let b = new B(10);
  46. b.say();
  47. // b.num= 20;
  48. console.log(b);
  49. b.num1 = 20;
  50. console.log(b);
  51. })();