Node: util.inherits does not establish prototype chain

Created on 7 Dec 2015  路  17Comments  路  Source: nodejs/node

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
doc util

Most helpful comment

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

All 17 comments

See also #3188

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.

_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 util.inherits in favor of new ES2015/harmony features and also in favor of external libraries that get the job done properly.

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.

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 :grin:

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 util.inherits with regards to the prototype chain.

+1

some documentation on the limitations of util.inherits

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.

For future reference, @eljefedelrodeodeljefe is referring to https://github.com/nodejs/node/pull/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.

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

@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 extends?

@joshxyzhimself extends doesn鈥檛 define super_, but otherwise, yes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

egoroof picture egoroof  路  90Comments

AkashaThorne picture AkashaThorne  路  207Comments

jonathanong picture jonathanong  路  93Comments

substack picture substack  路  878Comments

Trott picture Trott  路  87Comments