Person.js
Module.exports = function () {
THIS.name = "Person";
This.sleep = function () {
Console.log ("Sleep in the Night");
}
This.eat = function () {
Console.log (' eat food ');
}
}
Student.js
var util = require ("util");
var person = require ('./person ');
Defining the Student Class
function Student () {
Person.call (this);
}
Inherit student to Person
Util.inherits (Student,person);
Overriding the study method
Student.prototype.study = function () {
Console.log ("I am Learning");
}
Exposed Student class
Module.exports = Student;
Testinherits.js
var person = require ('./person ');
var Student = require ('./student ');
var personobj = new Person ();
var studentobj = new Student ();
Console.log ("-------for base class of person-------");
Personobj.sleep ();
Personobj.eat ();
Console.log ("---------------------------------");
Console.log ("-------for base class of student-------");
Studentobj.sleep ();
Studentobj.eat ();
Studentobj.study ();
Console.log ("---------------------------------");
Static call
var util = require ("util");
var person = require ('./person ');
Defining the Student Class
function Student () {
Person.call (this);
}
Inherit student to Person
Util.inherits (Student,person);
Overriding the study method
Student.prototype.study = function () {
Console.log ("I am Learning");
}
Exposing the method of the dynamic object directly to the Module.exports
var student= new student ();
Module.exports.study = Student.study;
Module.exports.eat = Student.eat;
Module.exports.sleep = Student.sleep;
Static call testing without instantiating the student object,
var Student = require ('./student ');
Student.sleep ();
Student.eat ();
Student.study ();
node. JS Inheritance