function _new(/* 构造函数 */ constructor, /* 构造函数参数 */ params) {
// 将 arguments 对象转为数组
var args = [].slice.call(arguments); // 取出构造函数
var constructor = args.shift(); // 创建一个空对象,继承构造函数的 prototype 属性
var context = Object.create(constructor.prototype); // 执行构造函数
var result = constructor.apply(context, args); // 如果返回结果是对象,就直接返回,否则返回 context 对象
return typeof result === 'object' && result != null ? result : context;
}
【JavaScript】手写new
于 2021-12-23 15:29:21 首次发布