zoukankan      html  css  js  c++  java
  • 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
    */
  • 相关阅读:
    Mysql 高可用方案讨论
    python 自动化之路 day 20 Django进阶/BBS项目【一】
    python 自动化之路 day 18 前端内容回顾、补充/Django安装、创建
    python 自动化之路 day 19 Django基础[二]
    Mysql配置文件读取顺序
    Mysql中查看每个IP的连接数
    循环杀死Mysql sleep进程脚本
    JS设计模式——5.单体模式(用了这么久,竟全然不知!)
    JS设计模式——4.继承(示例)
    JS设计模式——4.继承(概念)
  • 原文地址:https://www.cnblogs.com/dreamhome/p/8562247.html
Copyright © 2011-2022 走看看