1. class Singleton {
  2. constructor(x){
  3. const instance = this.constructor.instance;
  4. if(instance) {
  5. return instance;
  6. }
  7. this.constructor.instance = this;
  8. }
  9. }

Monostate 单态

  1. class ChiefExecutiveOfficer {
  2. get name(){
  3. return ChiefExecutiveOffice._name;
  4. }
  5. set name(value){
  6. ChiefExecutiveOffice._name = value;
  7. }
  8. get age(){
  9. return ChiefExecutiveOffice._age;
  10. }
  11. set age(value){
  12. ChiefExecutiveOffice._age = value;
  13. }
  14. toString(){
  15. return `CEO的名字是${this.name} ` + `ta的年龄是${this.age}岁`;
  16. }
  17. }
  18. ChiefExecutiveOfficer._name = undefined;
  19. ChiefExecutiveOfficer._age = undefined;