惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
C
Check Point Blog
B
Blog RSS Feed
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
小众软件
小众软件
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
GbyAI
GbyAI
The Cloudflare Blog
博客园 - 叶小钗
S
SegmentFault 最新的问题
博客园 - Franky
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
B
Blog
Jina AI
Jina AI

博客园 - howhy

TypeScript any vs unknown 详细对比 TypeScript interface vs type 完整对比 html 元素包含关系 this 指向 空对象 Object.keys for ...in Reflect.ownKeys ...区别 逻辑运算符和空值运算符 运算符优先级 js 类型显式转换 js 高级函数 js 方法重载 fetch timeout js 任务顺序执行 暂停 js 并发任务 判断两个对象是否相同 js 动态拦截属性 js 通用动画 js groupby js 防抖和节流 实现 instanceof 操作符 js 单例模式 js 数组去重和扁平方法 js 继承方法 js deepCopy js '=='的隐性类型转换规则
js new的过程实现
howhy · 2025-12-25 · via 博客园 - howhy
function myNew(constructor,...args){
    if(typeof constructor!=='function'){
        return new TypeError('constructor must be a function');
    }
    const newObj=Object.create(constructor.prototype);
    const instance=constructor.apply(newObj,args);
    return instance instanceof Object ? result : obj;
 }
function myNew(Constructor, ...args) {
    // 1. 创建一个空对象
    const obj = {};
    
    // 2. 设置对象的原型为构造函数的原型
    // 注意:__proto__ 不是标准属性,但现代浏览器都支持
    obj.__proto__ = Constructor.prototype;
    
    // 3. 调用构造函数,绑定 this
    const result = Constructor.apply(obj, args);
    
    // 4. 返回结果
    return result instanceof Object ? result : obj;
}