🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## new ``` function myNew(Func, ...args) { const instance = {}; if (Func.prototype) { Object.setPrototypeOf(instance, Func.prototype) } const res = Func.apply(instance, args) if (typeof res === "function" || (typeof res === "object" && res !== null)) { return res } return instance } // 测试 function Person(name) { this.name = name } Person.prototype.sayName = function() { console.log(`My name is ${this.name}`) } const me = myNew(Person, 'Jack') me.sayName() console.log(me) // My name is Jack let person = new Person("Jack"); console.log(person.sayName()); // undefined ```