Skip to content

js里使用单例模式

js
export default class HistoryKeyword {
  constructor() {
    if(typeof HistoryKeyword.instance === 'object'){
      return HistoryKeyword.instance
    }
    HistoryKeyword.instance = this
    return this
  }
}

在类里面的constructor里加上这样的代码,使用的时候,不管new多少次,都是操作的同一个对象

如有转载或 CV 的请标注本站原文地址