-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
util.inherits does not establish prototype chain #4179
Comments
See also https://phabricator.babeljs.io/T6765 |
See also #3188 |
That's because inherits() calls Perhaps it's time for inherits() to go gently into that good night and for us to point people to ES6 |
Sigh perhaps making it @deprecated would suffice then... |
Reopening as I deem it fit to at least document that shortcoming in the official documentation and deprecate |
We're open to pull requests that improve the documentation. As to deprecation, IMO it should start as a documentation-only deprecation that steers people away from using it in new code. |
Well I think that in the past, people did not care too much about As such, I think that with the next release of node the behavior should just be changed and let users just fix their code if they ever managed to screw it up so badly as to rely on that falsely set up prototype chain. |
@nodejs/documentation ... not sure about the deprecation here but it would likely be good to include some documentation on the limitations of |
+1
|
Reading up un this I think the change would actually be possible, no? @bnoordhuis there was this PR in Oct'15 that actually change the from Pointing the Documentation should only(?): 1. mention that it is overriding the prototype instead appending the chain 2. point to |
For future reference, @eljefedelrodeodeljefe is referring to #3455. |
Sorry meant to post the link. |
I am leaning towards class extends as solution for this one. Started a discussion at #6512 for docs. |
FYI this got closed due to a change in docs, see 07c572d. Thx for reporting! It triggered a good conversation and change. |
util.inherits breaks the prototype chain. A fix does not seem useful, since ES6 extends provides language level support for the same functionality. This commit starts fasing out mentions of the method. Fixes: #6512 Fixes: #4179 PR-URL: #6514 Reviewed-By: James Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
util.inherits breaks the prototype chain. A fix does not seem useful, since ES6 extends provides language level support for the same functionality. This commit starts fasing out mentions of the method. Fixes: #6512 Fixes: #4179 PR-URL: #6514 Reviewed-By: James Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
I know this is old, but I stumbled onto this looking for something else, and I think there's a little confusion in the claims of the original issue. First, as far as I'm concerned, this implementation of inherits is correct, or at least "good", with what we can call "room for improvement". The point of
This is your classic behavior with prototype chain inheritance. Instances of one constructor ( For this example:
It implies that the constructor itself would inherit from the super constructor. Generally this isn't done when setting up inheritance manually, but ES6 happens to do this with Now, this behavior is something that can be added on top of the existing behavior if desired. It's not meant to replace it. So "Object.setPrototypeOf is being called with the wrong parameters" is not really the correct way to explain what's going on. If you're expecting the constructor inheritance for static members, then it would be something done along side the existing instance inheritance. Object.setPrototypeOf(ctor.prototype, superCtor.prototype); // existing instance member inheritance
Object.setPrototypeOf(ctor, superCtor); // additional static inheritance |
@senocular does that mean that adding Object.setPrototypeOf(ctor, superCtor); below https://github.com/nodejs/node/blob/master/lib/util.js#L168 makes it work exactly like es6 |
@joshxyzhimself |
`util.inherits` is now deprecated due to issues with how it sets the prototype chain, see: nodejs/node#4179
In inherits.js Object.setPrototypeOf is being called with the wrong parameters.
The reason for me assuming that the wrong parameters are being used is
(REPL)
While inheritance sort of works, the prototype chain is actually never established.
Replacing the existing call to Object.setPrototypeOf() by
See https://github.com/nodejs/node/blob/master/lib/util.js#L805.
(REPL)
Yet, instanceof will now fail
When using the new class feature, everything works as expected, though
(REPL)
The text was updated successfully, but these errors were encountered: