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

推荐订阅源

V
Visual Studio Blog
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
博客园 - 叶小钗
The Cloudflare Blog
D
DataBreaches.Net
J
Java Code Geeks
G
Google Developers Blog
L
LangChain Blog
N
Netflix TechBlog - Medium
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
小众软件
小众软件
量子位
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
博客园_首页
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog
腾讯CDC

博客园 - 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;
}