Stdlib. js
// Package module. exports = stdlib; // declaration method stdlib. prototype. toString = toString; stdlib. prototype. talk = talk; // declare the variable stdlib. prototype. className = null; // constructor function stdlib (name) {this. className = 'class: '+ name; // gets the IO stream this. stdin = process. stdin; this. stdout = process. stdout ;}; // method function toString () {return this. className;}; function talk () {var interface = require ('readline'); var rl = interface. crea TeInterface (this. stdin, this. stdout); rl. setPrompt ('nerds> '); rl. prompt (); rl. on ('line', function (cmd) {switch (cmd. trim () {case': q! ': Console. log ('stream close! '); Process. exit (0); break; case 'girlfriend': console. log ('dude, I bet you won't: D'); break; default: console. log ('say what? I might have heard ''+ cmd. trim () + '''); break;} rl. prompt ();}). on ('close', function (cmd) {console. log ('Enjoy js world, have nice day: D'); process. exit (0 );});};
Main. js
// Install the module var stdlib = require ('. /stdlib '); // create the object var tsh = new stdlib ('My stdlib') from the module; console. log (tsh. toString (); tsh. talk ();
In node. js, modules. exports = class is used to expose a class to files installed on the corresponding module.
Lib.js:module.exports = Lib;function Lib() {}...UserFile.js 2:var installModule = module('./Lib');var instanceObject = installModule();...
Similar to java packaging and import packages
file 1:package com;public class Lib{.....}file 2:import com.lib;....