7.属性的封装.js 699 B

1234567891011121314151617181920212223242526272829
  1. (function () {
  2. /**
  3. * readonly
  4. * static
  5. * protected 受保护的:只能在当前类的子类中访问
  6. * public 共用的
  7. * private:只能在当前类进行使用和修改
  8. */
  9. class Person {
  10. constructor(name, age) {
  11. this.name = name;
  12. this.age = age;
  13. }
  14. getName() {
  15. return console.log;
  16. }
  17. get name1() {
  18. return this.name;
  19. }
  20. set name1(val) {
  21. this.name = val;
  22. }
  23. }
  24. let newPerSon = new Person('哆啦A梦', 7);
  25. newPerSon.name1 = '图图';
  26. console.log(newPerSon);
  27. console.log(newPerSon.name1);
  28. newPerSon.getName();
  29. })();