分析 普通函数执行和new函数执行的区别
一 new执行时函数内部会创建一个对象,并将this指向这个对象
二 函数内部this.xxx的操作都会在这个对象上添加属性
三 如果函数的返回值不是一个对象类型,则函数会将这个创建好的对象返回
代码一
function Dog(name) {
this.name = name;
}
Dog.prototype.bark = function () {
console.log('wangwang ');
}
Dog.prototype.sayName = function () {
console.log('my name is ' + this.name);
}
function _new(Ctor, ...params) {
let obj = {}
obj.__proto__ = Ctor.prototype
let result = Ctor.call(obj, ...params);
if (!result == null && /^(object|function)$/.test(typeof result)) return result
return obj
}
let tom = _new(Dog, 'tom')
tom.sayName()
优化后的代码
一 检测类型
二 考虑__proto__属性
function _new1(Ctor, ...params) {
let obj,
result,
proto,
ctorType;
proto = Ctor.prototype
ctorType = typeof Ctor
console.log(ctorType)
if(ctorType == 'symbol' || ctorType == 'bigint'|| ctorType != 'function' || !proto) throw new TypeError(`${Ctor} is not a constuctor`)
obj = Object.create(Ctor.prototype)
result = Ctor.call(obj, ...params);
if (!result == null && /^(object|function)$/.test(typeof result)) return result
return obj
}
let tom1 = _new1(Dog, 'tom1')
tom1.sayName()