6_存储器.js 941 B

1234567891011121314151617181920212223242526272829
  1. //存储器: 让我们可以有效的控制对于对象的成员的访问 通过getters或者setters进行操作
  2. (function () {
  3. var Person = /** @class */ (function () {
  4. function Person() {
  5. this.firstName = 'A';
  6. this.lastName = 'B';
  7. }
  8. Object.defineProperty(Person.prototype, "fullName", {
  9. get: function () {
  10. return this.firstName + '_' + this.lastName;
  11. },
  12. set: function (value) {
  13. var names = value.split('_');
  14. this.firstName = names[0];
  15. this.lastName = names[1];
  16. },
  17. enumerable: false,
  18. configurable: true
  19. });
  20. return Person;
  21. }());
  22. var p = new Person();
  23. console.log(p.fullName);
  24. p.firstName = 'C';
  25. p.lastName = 'D';
  26. console.log(p.fullName);
  27. p.fullName = 'E_F';
  28. console.log(p.firstName, p.lastName);
  29. })();