🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
1. 将new操作单独封装 2.遇到new时,就要考虑是否应该使用工厂模式。 ![](https://box.kancloud.cn/b6655508dee65f1baff03ff7e7425c6d_669x248.png) ![](https://img.kancloud.cn/08/29/08291d9e0cb70969dd9bf278f41272b0_610x523.png) ![](https://box.kancloud.cn/94e56d394e3617c627e3d83b3f04354e_620x309.png) ~~~ 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 creator = new Creator(); let p = new creator.create('p1'); p.init(); p.fun1(); ~~~ ![](https://img.kancloud.cn/c4/45/c44595d65193bf23e99236672745ca8b_523x405.png) ![](https://img.kancloud.cn/eb/73/eb7373402ba30597a2a10392af761bc7_574x571.png) ![](https://img.kancloud.cn/bc/3e/bc3e7786511b8b8398051a67b7396e12_843x391.png) ![](https://img.kancloud.cn/8d/59/8d5917a645ce2bbae4cd9262edec91bc_1160x346.png) ![](https://img.kancloud.cn/f5/3b/f53b343e9b8f3d523407097799233e06_879x390.png) ![](https://img.kancloud.cn/38/fd/38fd076933be00fd1f77c7c558c66a78_666x333.png)