为什么 Node.js 中的 util.inherits() 方法使用 Object.setPrototypeOf()?

Why does the util.inherits() method in Node.js uses Object.setPrototypeOf()?

我是 Node.js 的新手,但对 JavaScript 及其原型生态系统已经相当深入。我一般听到的是Object.setPrototypeOf(),或者给一个对象的__proto__赋一个新值属性,是不好的

这些 [[Prototype]] 突变会导致性能问题并导致代码优化程度较低。现在,当我试图以某种方式了解这个想法时,我看到 Node.js 中的 util.inherits() 方法使用 Object.setPrototypeOf() 来设置第一个 arg 的原型。可以在 lib/util.js.

找到源代码

如果 Object.setPrototypeOf() 真的那么糟糕,那么 Node.js 使用它来配置给定对象的原型的原因是什么?

而且,如果我在给定脚本的开头调用它,Object.setPrototypeOf() 是否仍然具有破坏性,并且注意不要在运行时调用它?

就像setPrototypeOf is discouraged for the reasons you give, so is util.inherits -- for the same reasons. Quoted from the documentation:

Usage of util.inherits() is discouraged. Please use the ES6 class and extends keywords to get language level inheritance support. Also note that the two styles are semantically incompatible.

这还建议您应该如何避免 setPrototypeOfutil.inherits

MDN 添加此建议:

if you are concerned about performance, you should avoid setting the [[Prototype]] of an object. Instead, create a new object with the desired [[Prototype]] using Object.create().