[Javascript] OOP 'terminal & amp; #39; demo

Source: Internet
Author: User

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;....


 

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.