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

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
Stack Overflow Blog
Stack Overflow Blog
量子位
腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
S
SegmentFault 最新的问题
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
G
Google Developers Blog
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
雷峰网
雷峰网
罗磊的独立博客
Vercel News
Vercel News
L
LangChain Blog
V
V2EX
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
博客园 - Franky
V
Visual Studio Blog
J
Java Code Geeks

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