美文网首页HTML的自我修养瓦斯程序媛node
终于讲清楚了nodejs中exports和module.expo

终于讲清楚了nodejs中exports和module.expo

作者: 程序鱼 | 来源:发表于2017-01-14 11:06 被阅读535次

    最近正在学习nodejs,看到nodejs模块这块,发现nodejs模块有两种方式对外暴露方法
    exports和module.exports

    可是这两种使用起来到底有什么区别呢???

    看了很多文章,长篇大论,始终没有讲清楚区别,自己也是看了很多,终于搞清楚了,给大家分享一下

    根据使用方法来说

    通常exports方式使用方法是:

    exports.[function name] = [function name]

    moudle.exports方式使用方法是:

    moudle.exports= [function name]

    这样使用两者根本区别是

    **exports **返回的是模块函数

    **module.exports **返回的是模块对象本身,返回的是一个类

    使用上的区别是
    exports的方法可以直接调用
    module.exports需要new对象之后才可以调用

    二话不说,撸代码!

    1. exports方式

    先创建一个exports_mode.js

    var sayHello = function(){
        console.log('hello')
    }
    exports.sayHello = sayHello
    
    console.log(exports); 
    console.log(module.exports); 
    

    然后写一个test.js调用下试试看

    var exports_mode = require('./exports_mode')
    exports_mode.sayHello()
    

    输出:


    exports_mode.png

    发现此时exports和module.exports对象输出的都是一个sayHello方法,
    为什么module.exports也有exports方法了,简单点理解就是

    exports是module.exports的一个引用,exports指向的是module.exports

    我们来验证下,在exports_mode.js最后一行添加一句代码

    var sayHello = function(){
        console.log('hello')
    }
    exports.sayHello = sayHello
    
    console.log(exports); 
    console.log(module.exports); 
    console.log(exports === module.exports); 
    
    结果输出.png

    发现console.log(exports === module.exports)返回的是true,
    说明exports和module.exports是同一个对象

    下来看看

    2. module.exports方式

    首先创建module_exports_mode.js

    var sayHello = function(){
        console.log('hello')
    }
    module.exports = sayHello
    
    console.log(module.exports); 
    console.log(exports); 
    console.log(exports === module.exports); 
    

    然后测试一下

    var module_export_mode = require('./module_exports_mode')
    module_export_mode.sayHello()
    
    控制台输出.png

    发现输出报错了!

    为什么呢,因为我们的调用方式错了,一开始就说到了

    **module.exports **返回的是模块对象本身

    正确的调用

    var module_export_mode = require('./module_exports_mode')
    new module_export_mode()
    
    控制台输出.png

    同时我们可以看到,输出的module.exports对象内容就是一个[Function],在javascript里面是一个类

    使用这样的好处是exports只能对外暴露单个函数,但是module.exports却能暴露一个类

    我们把module_exports_mode.js扩展一下

    var xiaoming = function(name){
        this.name = name
    
        this.sayHello = function(){
            return 'hello '+this.name
        }
        this.sayGoodBye = function(){
            return 'goodbye '+this.name
        }
    }
    module.exports = xiaoming
    
    console.log(module.exports); 
    console.log(exports); 
    console.log(exports === module.exports); 
    

    然后测试

    var xiaoming = require('./module_exports_mode')
    var xiaoming = new xiaoming('Lucien')
    console.log(xiaoming.sayHello())
    console.log(xiaoming.sayGoodBye())
    
    控制台输出.png

    使用方法和javascript的类创建对象一毛一样

    exports.[function name] = [function name]
    moudle.exports= [function name]

    以上就是这两种方式的使用区别。


    等等,还没完。。。

    上面有提到

    exports是module.exports的一个引用,exports指向的是module.exports

    也就是说exports的方法module.exports也是一定能完成的

    exports.[function name] = [function name]
    moudle.exports= [function name]

    所以,在使用上

    ** moudle.exports.[function name] = [function name] **
    ** 是完全和 **
    ** exports.[function name] = [function name] **
    ** 相等的 **

    但是我们通常还是推荐使用exports.[function name],各司其职,代码逻辑清晰

    相关文章

      网友评论

      • 4261558825a8:感谢楼主分享,茅舍顿开的感觉。
      • 9591639b7547:exports本身只是为了简写module.exports而设置的一个临时变量,二者完全一样。
        module.exports就是导出的对象,module.export['function name']就是在导出的对象上加一个方法。
        倒不是向楼上说的你讲错了,而是本来清清楚楚简简单单的一个模块,新手的话绝对被你绕晕。
      • 2df8646cf919:这篇文章完完全全,彻彻底底的是错的!!! 请大家不要被误导了。
        程序鱼:@乐希沐千 我的理解是exports是module.exports的一个引用,exports指向的是module.exports
        程序鱼:@乐希沐千 嗯哼,希望兄台不吝赐教,可不可以举例说明下,我的错误点
        2df8646cf919:这句话完全是错误的 “exports 返回的是模块函数

        module.exports 返回的是模块对象本身,返回的是一个类”


        官网的例子一目了然:

        function require(/* ... */) {
        const module = { exports: {} };
        ((module, exports) => {
        // Module code here. In this example, define a function.
        function someFunc() {}
        exports = someFunc;
        // At this point, exports is no longer a shortcut to module.exports, and
        // this module will still export an empty default object.
        module.exports = someFunc;
        // At this point, the module will now export someFunc, instead of the
        // default object.
        })(module, module.exports);
        return module.exports;
        }
      • 小九喵喵:有点晕...
        程序鱼: @erichow 👍是这个理
        erichow:var foo = {};
        var ref = foo;
        ref.a = 1;
        ref.b = 2;
        ref === foo // true;

        ref = {a:1, b, 2};
        ref === foo // false
        程序鱼:@themachine15 是因为我没有讲清楚吗……

      本文标题:终于讲清楚了nodejs中exports和module.expo

      本文链接:https://www.haomeiwen.com/subject/wedebttx.html