Node js模块系统

/*
模块是Node.js 应用程序的基本组成部分,文件和模块是对应的。换言之,一个  js 文件就是一个模块,这个文件可能是JavaScript 代码、JSON 或者编译过的C/C++ 扩展。

一个模块可理解成一个类文件,通过require引入这个类文件并实例化一个对象,类文件中只有通过exports定义的方法变量才可被实例化对象调用,正常定义的方法变量只能在类内部互相调用
并且正常定义方法变量名可与exports定义的重名,这里为避免混淆,用不同命名


以下实例中,代码 require('./hello') 引入了当前目录下的 hello.js 文件(./ 为当前目录,node.js 默认后缀为 js)
之前通过require包含调用的都是node原生模块,如require("http");
require方法接受以下几种参数的传递:
http、fs、path等,原生模块。
./mod或../mod相对路径的文件模块。
/pathtomodule/mod绝对路径的文件模块。
mod非原生模块的文件模块。
 */
//===============================以下代码为main.js文件中内容==========================================

var hello = require('./hello');
hello.testFun1('use testFun1');
console.log(hello.testVal1);

//===============================以上代码为main.js文件中内容==========================================


//===============================以下代码为hello.js文件中内容==========================================

exports.testFun1 = function(setVal) {
    exports.testVal1 = setVal;
    console.log('Hello ' + exports.testVal1);
    testFun2();
    exports.testFun3();
}

function testFun2(){
    console.log('Hello ' + testVal2);
}

exports.testFun3 = function() {
    console.log('Hello ' + 'this is testFun3');
}

exports.testVal1 = 'this is testVla1';
testVal2 = 'this is testVla2';

//===============================以上代码为hello.js文件中内容==========================================

/*
执行main.js
node main.js
输出内容:
Hello use testFun1
Hello this is testVla2
Hello this is testFun3
use testFun1
*/


/*

有时候想把一个类对象封装到模块中,即一个模块代表一个类
模块js文件中可以写成如下格式
module.exports = function() {
    // ...
}
或
function testName() { //testName在这里只起到方法命名及给module.exports赋值作用,在调用页main.js中无用
    // ...
};
module.exports = testName;

在外部引用该模块时,给变量赋值的就是模块类本身(该类需要被实例化使用),而不是原先的直接的实例化对象。
例
*/
//===============================以下代码为main.js文件中内容==========================================

var TestClassName = require('./hello');
hello = new TestClassName();
hello.setName('风吹屁股凉冰冰');
hello.getName();
console.log(hello.name1);

//===============================以上代码为main.js文件中内容==========================================


//===============================以下代码为hello.js文件中内容==========================================

function ClassName() {
    this.name1 = ''; //用 this. 定义的变量和方法,在类内部使用时前面也需加this.。并且可被实例化的对象调用
    var  name2;      //用 var 定义的变量或直接定义的方法,类内部使用时可直接用,前面不用加this. 但不可被类实例化对象调用。并且用 var 定义的变量和直接定义的方法可与 this. 定义的重名,这里为避免混淆,用不同命名。
    this.trueName = '';
    this.setName = function(theName) {
        this.trueName = theName;
    };
    this.getName = function() {
        this.test1();
        test2();
        console.log('Hello ' + this.name1 + ' ' + name2 + ' ' + this.trueName);
    };
    this.test1 = function(){
        this.name1 = 'test1Name';
    }
    function test2(){
        name2 = 'test2Name';
    }
};
module.exports = ClassName;

//===============================以上代码为hello.js文件中内容==========================================
/*
执行main.js
node main.js
输出内容:
Hello test1Name test2Name 风吹屁股凉冰冰
test1Name
*/

 

转载于:https://www.cnblogs.com/dreamhome/p/8562247.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值