ChickenDreamFactory / fe-chicken

✨✨✨ 集锦 前端JavaScript 手写题,编程题,Not just for interviews

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

60.new

webVueBlog opened this issue · comments

// new 运算符
// 作用:负责实例化一个类(构造函数)
// 原理:
// 1. 创建一个构造函数原型对象为原型的对象
// 2. 以第一步的对象为上下文执行构造函数
// 3. 返回值,如果函数有返回值,则返回函数的返回值,否则返回第一步创建的对象。

// Function 构造函数
// Array 构造函数的其他参数组成数组
// 对象实例
module.exports = function newOperator(constructor, ...args) {
 const ins = Object.create(constructor.prototype)
 const res = constructor.apply(ins, args)
 return res || ins
}