function myNew(){
var constructor = [].shift.call(arguments),
_this = {};
_this.__proto__ = constructor.prototype;
var res = constructor.apply(_this,arguments);
return return typeof res === 'object' ? res : _this;
}
function instanceof(target,type){
// target 实例化对象 type 构造函数
type = type.prototype;
target = target.__proto__;
while(true){
if(target === null){
return false;
}
if(target === type){
target = target.__proto__;
}
}
}
手写new,instanceof方法
于 2022-12-22 15:36:10 首次发布
本文解析了JavaScript函数`myNew`,它利用`.shift()`和`.apply()`实现构造函数的实例化,并介绍了`instanceof`用于判断对象是否由特定构造函数创建。通过实例演示了如何利用原型链进行继承和类型检查。
437

被折叠的 条评论
为什么被折叠?



