jimpix
jimpix

Reputation: 147

Why is the usage of util.inherits() discouraged?

According to the Node.js documentation :

Note: usage of util.inherits() is discouraged. Please use the ES6 class and extends keywords to get language level inheritance support. Also note that the two styles are semantically incompatible.

https://nodejs.org/api/util.html#util_util_inherits_constructor_superconstructor

Upvotes: 2

Views: 1941

Answers (3)

Basavaraj SK
Basavaraj SK

Reputation: 499

util.inherits() got deprecated in the new version of node so need to use the ES6 class and extends keywords to get language level inheritance support instead of utils.inherits.

below example which I gave below helps you to understand more clearly :

"use strict";

class Person {
  constructor(fName, lName) {
    this.firstName = fName;
    this.lastName = lName;
  }

  greet() {
    console.log("in a class fn..", this.firstName, "+ ", this.lastName);
  }
}

class PoliceMan extends Person {
  constructor(burgler) {
    super("basava", "sk");
    this.burgler = burgler;
  }
}

let policeObj = new PoliceMan();

policeObj.greet();

Output : in a class fn.. basava +  sk

Here we can see Person class is inherited by PoliceMan class, so that PoliceMan obj can access the properties of Person class by calling super(); in a constructor

Hope this will work as util.inherits();

Happy Coding !!!

Upvotes: 0

trincot
trincot

Reputation: 350242

The reason why util.inherits is discouraged, is because changing the prototype of an object should be avoided, as most JavaScript engines look for optimisations assuming that the prototype will not change. When it does, this may lead to bad performance.

util.inherits relies on Object.setPrototypeOf to make this change, and the MDN documentation of that native method has this warning:

Warning: Changing the [[Prototype]] of an object is, by the nature of how modern JavaScript engines optimize property accesses, currently a very slow operation in every browser and JavaScript engine. In addition, the effects of altering inheritance are subtle and far-flung, and are not limited to the time spent in the Object.setPrototypeOf(...) statement, but may extend to any code that has access to any object whose [[Prototype]] has been altered.

Because this feature is a part of the language, it is still the burden on engine developers to implement that feature performantly (ideally). Until engine developers address this issue, if you are concerned about performance, you should avoid setting the [[Prototype]] of an object. Instead, create a new object with the desired [[Prototype]] using Object.create().

Upvotes: 2

skypjack
skypjack

Reputation: 50540

As the quote says, you should use the ES6 class and extends keywords to get language level inheritance support instead of utils.inherits and that's exactly the reason for which to use it is discouraged: there exist better alternatives that are part of the core language, that's all.
util.inherits comes from the time when those utils were not part of the language and it requires you a lot of boilerplate to define your own inheritance tools.
Nowadays the language offers a valid alternative and it doesn't make sense anymore to use the ones provided with the library itself. Of course, this is true as long as you use plan to use ES6 - otherwise ignore that note and continue to use utils.inherits.


To reply to your comment:

How is util.inherits() more complicated?

It's not a matter of being more or less complicated. To use a core language feature should be ever your preferred way over using a library specific alternative for obvious reasons.

Upvotes: 2

Related Questions