Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A Question Regarding "util.inherits" #20980

Closed
BastiDood opened this issue May 26, 2018 · 4 comments
Closed

A Question Regarding "util.inherits" #20980

BastiDood opened this issue May 26, 2018 · 4 comments
Labels
question Issues that look for answers. util Issues and PRs related to the built-in util module.

Comments

@BastiDood
Copy link

  • Version: v10.2.1 (Current)
  • Platform: 64-bit Windows 10 v1803 (OS Build 17134.81)
  • Subsystem: util

Attached below is the permalink of the code for the inherits method of the util module.

node/lib/util.js

Lines 1170 to 1184 in 39f2096

function inherits(ctor, superCtor) {
if (ctor === undefined || ctor === null)
throw new ERR_INVALID_ARG_TYPE('ctor', 'Function', ctor);
if (superCtor === undefined || superCtor === null)
throw new ERR_INVALID_ARG_TYPE('superCtor', 'Function', superCtor);
if (superCtor.prototype === undefined) {
throw new ERR_INVALID_ARG_TYPE('superCtor.prototype',
'Function', superCtor.prototype);
}
ctor.super_ = superCtor;
Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
}

As of opening this issue, line 1183 baffles my mind. Why would Node.js use Object.setPrototypeOf to set the prototypes of the classes/objects? I have heard from MDN that Object.setPrototypeOf is slower and less efficient than if one had just done this instead:

ctor.prototype = Object.create(superCtor.prototype);

Although the performance change is insignificant, I still believe that this proposal must be made. Thanks!

@devsnek
Copy link
Member

devsnek commented May 26, 2018

its actually setting the prototype of the prototype of the ctor, not the prototype of the ctor. the other way to do that would be ctor.prototype.__proto__ = Object.create(superCtor.prototype) and Object.setPrototypeOf is just a better way of doing that. either way its replacing the prototype which means the engine has to reoptimise everything.

@devsnek devsnek added question Issues that look for answers. util Issues and PRs related to the built-in util module. labels May 26, 2018
@BastiDood
Copy link
Author

If it's not too much to ask, why does one have to set the prototype of the prototype of the inheriting object? I'm still learning the ropes of JavaScript, so I'd love to learn as much as I can.

@benjamingr
Copy link
Member

benjamingr commented May 27, 2018

It used to work this way, but there was a bug with making it work with ES2015 classes (which don't allow setting .prototype which doing this fixed.

See relevant changes here: 29da8cf

Here is the relevant bug #3452

@BastiDood
Copy link
Author

Oh, I see now. Thanks for the clarification! I appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

3 participants