xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub! js & class & init

how to call class init method in js when create an instance

在初始化类实例的时候调用,类构造函数或 init 方法

1. 类实例化的时候,会自动执行 constructor 里面操作

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2020-02-12
 * @modified
 *
 * @description PolygonConvert class
 * @augments
 * @example
 * @link
 *
 */

const log = console.log;

class PolygonConvert {
  constructor(poly) {
    this.poly = poly;
    this.name = `polygon`;
    // auto call init
    // PolygonConvert.init();
    this.onEvent();
  }
  // static init() {
  //   log(`init polygon`);
  //   this.svgEventBus.$on(`update-status-data`, this.updateSVGData);
  // }
  onEvent() {
    this.svgEventBus.$on(`update-status-data`, this.updateSVGData);
  }
  updateSVGData() {
    log(`update datas`);
  }
  getName() {
    return this.name;
  }
  getDOM() {
    return this.poly;
  }
   // ...
}

export {
  PolygonConvert,
};

export default PolygonConvert;


https://*.com/questions/3526916/javascript-class-call-method-when-object-initialized