Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement inheritance in Node.JS

How do we use 'inheritance' in Node.JS? I heard that prototype is similar to interfaces in java. But I have no idea how to use it!

like image 594
user1190937 Avatar asked Dec 09 '25 00:12

user1190937


1 Answers

Although there are various ways of performing inheritance and OO in javascript, in Node.js you would typically use the built in util.inherits function to create a constructor which inherits from another.

See http://book.mixu.net/ch6.html for a good discussion on this subject.

for example:

var util = require("util");
var events = require("events");

function MyOwnClass() {
    // ... your code.
}

util.inherits(MyOwnClass, events.EventEmitter);
like image 57
AndyD Avatar answered Dec 11 '25 13:12

AndyD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!