javascript - Object.Create() 在幕后做了什么?

标签 javascript prototypal-inheritance

我正在深入研究 JavaScript 的原型(prototype)继承。当 Object.Create() 用于创建对象时,有人可以展示幕后发生的事情吗? Object.Create() 是否依赖于幕后的 new 和构造函数?

最佳答案

When Object.create() is in use to create objects, can someone show what is going on under the hood?

底层细节。 Object.create 几乎是一个原始操作 - 类似于评估 {} 对象文字时发生的情况。试着理解what it is doing .

也就是说,通过新的 ES6 操作,它可以根据

function create(proto, descriptors) {
    return Object.defineProperties(Object.setPrototypeOf({}, proto), descriptors);
}

Does Object.create() depend on new and constructor functions behind the scenes?

不,一点也不。恰恰相反。 new 运算符可以实现为

function new(constructor, arguments) {
    var instance = Object.create(constructor.prototype);
    constructor.apply(instance, arguments);
    return instance;
}

关于javascript - Object.Create() 在幕后做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49116846/

相关文章:

javascript - 如何使用 jquery 检查一行中的所有文本框是否已填充

javascript - webpackDevMiddleware 不会自动重新加载

javascript - 从 Google Maps API 返回的 jQuery 格式时间

javascript - Object.create() 和 toString()

javascript - 如何编写检查数组数组的过滤器

javascript - Mongoose findOneAndUpdate 和 upsert 不返回错误,不影响文档

javascript oop 访问从基础扩展 'class' 成员

javascript - ECMAScript 规范是否允许 Array 为 "superclassable"?

javascript - 从其他类继承原型(prototype)方法而不覆盖自己的原型(prototype)方法

javascript - Object.create 与直接原型(prototype)继承