Skip to content Skip to sidebar Skip to footer

Javascript Oop In Nodejs: How?

I am used to the classical OOP as in Java. What are the best practices to do OOP in JavaScript using NodeJS? Each Class is a file with module.export? How to create Classes? this.C

Solution 1:

This is an example that works out of the box. If you want less "hacky", you should use inheritance library or such.

Well in a file animal.js you would write:

var method = Animal.prototype;

functionAnimal(age) {
    this._age = age;
}

method.getAge = function() {
    returnthis._age;
};

module.exports = Animal;

To use it in other file:

varAnimal = require("./animal.js");

var john = newAnimal(3);

If you want a "sub class" then inside mouse.js:

var _super = require("./animal.js").prototype,
    method = Mouse.prototype = Object.create( _super );

method.constructor = Mouse;

functionMouse() {
    _super.constructor.apply( this, arguments );
}
//Pointless override to show super calls//note that for performance (e.g. inlining the below is impossible)//you should do//method.$getAge = _super.getAge;//and then use this.$getAge() instead of super()
method.getAge = function() {
    return _super.getAge.call(this);
};

module.exports = Mouse;

Also you can consider "Method borrowing" instead of vertical inheritance. You don't need to inherit from a "class" to use its method on your class. For instance:

var method = List.prototype;
 functionList() {

 }

 method.add = Array.prototype.push;

 ...

 var a = newList();
 a.add(3);
 console.log(a[0]) //3;

Solution 2:

As Node.js community ensure new features from the JavaScript ECMA-262 specification are brought to Node.js developers in a timely manner.

You can take a look at JavaScript classes. MDN link to JS classes In the ECMAScript 6 JavaScript classes are introduced, this method provide easier way to model OOP concepts in Javascript.

Note : JS classes will work in only strict mode.

Below is some skeleton of class,inheritance written in Node.js ( Used Node.js Version v5.0.0 )

Class declarations :

'use strict'; 
classAnimal{

 constructor(name){
    this.name = name ;
 }

 print(){
    console.log('Name is :'+ this.name);
 }
}

var a1 = newAnimal('Dog');

Inheritance :

'use strict';
classBase{

 constructor(){
 }
 // methods definitions go here
}

classChildextendsBase{
 // methods definitions go hereprint(){ 
 }
}

var childObj = newChild();

Solution 3:

I suggest to use the inherits helper that comes with the standard util module: http://nodejs.org/api/util.html#util_util_inherits_constructor_superconstructor

There is an example of how to use it on the linked page.

Solution 4:

This is the best video about Object-Oriented JavaScript on the internet:

The Definitive Guide to Object-Oriented JavaScript

Watch from beginning to end!!

Basically, Javascript is a Prototype-based language which is quite different than the classes in Java, C++, C#, and other popular friends. The video explains the core concepts far better than any answer here.

With ES6 (released 2015) we got a "class" keyword which allows us to use Javascript "classes" like we would with Java, C++, C#, Swift, etc.

Screenshot from the video showing how to write and instantiate a Javascript class/subclass: enter image description here

Solution 5:

In the Javascript community, lots of people argue that OOP should not be used because the prototype model does not allow to do a strict and robust OOP natively. However, I don't think that OOP is a matter of langage but rather a matter of architecture.

If you want to use a real strong OOP in Javascript/Node, you can have a look at the full-stack open source framework Danf. It provides all needed features for a strong OOP code (classes, interfaces, inheritance, dependency-injection, ...). It also allows you to use the same classes on both the server (node) and client (browser) sides. Moreover, you can code your own danf modules and share them with anybody thanks to Npm.

Post a Comment for "Javascript Oop In Nodejs: How?"