js工程模式

按照某种形式批量生成对象:

class Product {
  constructor(name) {
    this.name = name;
  }
  init() {
    alert('init');
  }
  fun1() {
    alert('fun1');
  }
  fun2() {
    alert('fun2');
  }
}
class Creator {
  create(name) {
    return new Product(name);
  }
}

// let factory = new Creator();
// let obj = factory.create('hxy');
// obj.init();
window.createObj = name => {
  return new Product(name);
};
createObj('hxy').init();


实例:
jQuery中的$
js工程模式_第1张图片
React.createElement()
js工程模式_第2张图片

你可能感兴趣的:(设计模式)