我正在深入研究 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 onnew
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/