util.inherits does not establish prototype chain #4179

Closed
silkentrance opened this Issue Dec 7, 2015 · 15 comments

Projects

None yet

8 participants

@silkentrance

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)

> function MyError() {}
> util.inherits(MyError, Error);
> MyError
{ [Function: MyError]
  super_: 
   { [Function: Error]
     captureStackTrace: [Function: captureStackTrace],
     stackTraceLimit: 10 } }
> Error.isPrototypeOf(MyError);
false

While inheritance sort of works, the prototype chain is actually never established.

> myerr = new MyError()
[Error]
> myerr instanceof MyError
true
> myerr instanceof Error
true

Replacing the existing call to Object.setPrototypeOf() by

Object.setPrototypeOf(ctor, superCtor);

See https://github.com/nodejs/node/blob/master/lib/util.js#L805.

(REPL)

> function MyError() {}
> util.inherits(MyError, Error);
> MyError
[Function: OError]
> Error.isPrototypeOf(MyError);
true

Yet, instanceof will now fail

> myerr = new MyError()
[Error]
> myerr instanceof MyError
true
> myerr instanceof Error
false

When using the new class feature, everything works as expected, though

(REPL)

> class MyError extends Error {}
[Function: MyError]
> Error.isPrototypeOf(MyError)
true
> myerr = new MyError()
[Error]
> myerr instanceof MyError
true
> myerr instanceof Error
true
@silkentrance silkentrance changed the title from util.inherits calls Object.setPrototypeOf with the wrong parameters to util.inherits does not establish prototype chain Dec 7, 2015
@mscdex mscdex added the util label Dec 7, 2015
@silkentrance

See also #3188

@bnoordhuis
Member

Object.setPrototypeOf(ctor, superCtor)

That's because inherits() calls Object.setPrototypeOf(ctor.prototype, superCtor.prototype). It goes all the way back to v0.3.2 so I don't think we can change that without breaking a lot of existing code.

Perhaps it's time for inherits() to go gently into that good night and for us to point people to ES6 extends.

@silkentrance

Sigh perhaps making it @deprecated would suffice then...

@silkentrance

Reopening as I deem it fit to at least document that shortcoming in the official documentation and deprecate util.inherits in favor of new ES2015/harmony features and also in favor of external libraries that get the job done properly.

@silkentrance silkentrance reopened this Jan 31, 2016
@bnoordhuis
Member

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. util.inherits() is used in a lot of existing code and is, for the most part, not broken. Printing deprecation warnings is arguably too aggressive / obnoxious.

@silkentrance silkentrance referenced this issue in jashkenas/coffeescript Feb 2, 2016
Open

Establish a proper prototype chain #4197

@silkentrance

Well I think that in the past, people did not care too much about isPrototypeOf, so I think it would be safe to fix util.inherits. Considering that most users would test recursively for _super. See also the vibejs-subclassof project, which BTW did not gain that much traction 😁

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.

@jasnell
Member
jasnell commented Apr 12, 2016

@nodejs/documentation ... not sure about the deprecation here but it would likely be good to include some documentation on the limitations of util.inherits with regards to the prototype chain.

@lordKnighton
Member

+1

some documentation on the limitations of util.inherits

@eljefedelrodeodeljefe
Contributor

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 .create() to .setPrototypeOf(). Though I don't mind the missing chain, the change wouldn't breaking then, or is it?

Pointing the extends for real inheritance sounds good to me, though I wouldn't deprecate, since it actually works well.

Documentation should only(?): 1. mention that it is overriding the prototype instead appending the chain 2. point to extends in order to the chain.

@bnoordhuis
Member

For future reference, @eljefedelrodeodeljefe is referring to #3455.

@eljefedelrodeodeljefe
Contributor

Sorry meant to post the link.

@eljefedelrodeodeljefe
Contributor

I am leaning towards class extends as solution for this one. Started a discussion at #6512 for docs.

@eljefedelrodeodeljefe eljefedelrodeodeljefe added a commit to eljefedelrodeodeljefe/node that referenced this issue May 2, 2016
@eljefedelrodeodeljefe eljefedelrodeodeljefe doc: discourage use of util.inherits; point to es6 extends
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: #4179
e5f6fef
@eljefedelrodeodeljefe eljefedelrodeodeljefe added a commit that closed this issue May 9, 2016
@eljefedelrodeodeljefe eljefedelrodeodeljefe doc: discourage use of util.inherits; point to es6 extends
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: #6514
Reviewed-By: James Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
07c572d
@eljefedelrodeodeljefe
Contributor
eljefedelrodeodeljefe commented May 9, 2016 edited

FYI this got closed due to a change in docs, see 07c572d. Thx for reporting! It triggered a good conversation and change.

@eljefedelrodeodeljefe eljefedelrodeodeljefe added a commit that referenced this issue May 9, 2016
@eljefedelrodeodeljefe eljefedelrodeodeljefe doc: discourage use of util.inherits
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 <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
9daf4a2
@evanlucas evanlucas added a commit that referenced this issue May 17, 2016
@eljefedelrodeodeljefe @evanlucas eljefedelrodeodeljefe + evanlucas doc: discourage use of util.inherits
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 <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
d49b49e
@tangxinfa tangxinfa added a commit to tangxinfa/node that referenced this issue Jun 17, 2016
@eljefedelrodeodeljefe @tangxinfa eljefedelrodeodeljefe + tangxinfa doc: discourage use of util.inherits
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: nodejs#6512
Fixes: nodejs#4179

PR-URL: nodejs#6514
Reviewed-By: James Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
57d9a30
@senocular
senocular commented Aug 15, 2016 edited

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 inherits is to allow instances of one constructor to inherit members that are available to instances of another (those members stored in the prototype). This is what the current implementation achieves, as seen with the original example with:

> myerr = new MyError()
[Error]
> myerr instanceof MyError
true
> myerr instanceof Error
true

This is your classic behavior with prototype chain inheritance. Instances of one constructor (MyError) are able to access members of another (Error) and pass the instanceof check (which compares prototypes).

For this example:

> function MyError() {}
> util.inherits(MyError, Error);
> MyError
{ [Function: MyError]
  super_: 
   { [Function: Error]
     captureStackTrace: [Function: captureStackTrace],
     stackTraceLimit: 10 } }
> Error.isPrototypeOf(MyError);
false

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 extends as it allows static members to be inherited. Prior to ES6, I don't think I've ever seen anyone ever do this (plus prior to ES6 and Object.setPrototypeOf, it was only possible going through __proto__ which you should feel dirty for even considering using ;P).

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
@sam-github sam-github added doc and removed doc labels Dec 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment