-
Notifications
You must be signed in to change notification settings - Fork 41
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
chore: remove inherits by migrating to ES6 class syntax #270
Conversation
Why? Is there a discussion in an issue I may have missed where it was decided this is desired? |
Oh, there's no previous discussion about it, although a similar attempt was tried a while ago: #236. It's more a fix on my end that I hope to be a contribution with mutual benefit. |
What is it fixing? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't do refactors like these when they aren't desperately needed, it always risks introducing bugs without adding much
You can use the following two to remove the dependency on |
For reference, the linked example is a Node.js specific implementation of the JavaScript native way to define the prototype chain. Refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain#building_longer_inheritance_chains. |
The suggested approach to remove the dependency still won't resolve the issue (from another JavaScript runtime, which is Node.js-like but not Node.js), the situation is: Using class Foo {
constructor () {
this.name = 'foo'
}
}
function Bar() {
this.version = '0.0.0'
if (!new.target) return new Bar()
}
Object.setPrototypeOf(Bar.prototype, Foo.prototype)
Object.setPrototypeOf(Bar, Foo)
console.log(Bar().name) // <- undefined That's true that we (maintainers from that alternative JavaScript runtime) could make workarounds to support The comment from @gurgunday is very straightforward and reasonable, with everything well clarified, I think we can close the PR if I am not missing something. |
What is that issue? I haven't seen a description or minimal reproduction of a bug. |
I've updated the comment, it's hard to give the right amount of context, ironically now I see that probably I gave too much, haha. Sorry. |
This code is spec compliant JavaScript. I think it is a runtime bug if you are having issues with it on a specific runtime. |
That's true, the current avvio implementation is a perfect spec-wise javascript code, but not so spec-wise with the documentation from the current Node.js LTS version, there we see EventEmitter being defined as a class, also all examples initialize EventEmitters with the I realized that I should have created an issue first before actually writing the code, it won't happen again. Lastly, there's no strong feeling about this, any decision will be smoothly respected. :) |
I would actually recommend to update your runtime to lift the EventEmitter implementation from Node.js: https://github.com/nodejs/node/blob/main/lib/events.js You'll have far less compatibility issues. |
I don't understand how this affects the standard code we have in this repo. The Node.js I am closing this issue because it does not benefit the project and does not seem to fix a bug. All evidence points to a bug elsewhere. If you're trying to implement a Node.js compatible event emitter, I suggest reviewing the above linked code. An example in the documentation is not a specification for the implementation. If that's not what you're trying to do, I suggest reviewing how your custom runtime handles prototype chains and the class sugar. |
Not a problem, life certainly finds its path, and again, thanks for the advice and time of each of you. |
Just for clarity fro anybody that stumble on this in the future, I have done a simple benchmark of classes vs functions, and apparently, classes are measurably slower at instantiation https://github.com/mcollina/class-vs-function. |
@mcollina That's not the case on my end:
|
Refactor to remove the usage of
util.inherits
in favor of the recommended approach (https://nodejs.org/docs/v22.11.0/api/util.html#utilinheritsconstructor-superconstructor).Unfortunately, the git diff got a bit confused because of the indentation level change. The PR is mainly an adaptation from one syntax to another, the more opinionated points are:
new
keyword.new
, this piece of code was removed:The motivation of this PR is interoperability with an alternative JS runtime. I am trying to execute
avvio
along with an EventEmitter implementation that is ES6 class-based.Checklist
npm run test
andnpm run benchmark
and the Code of conduct