node. JS Inheritance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.