zoukankan      html  css  js  c++  java
  • Atitit node.js自定义模块化 Function walkFileTree() exports.walkFileTree =walkFileTree 引用  Files=require

    Atitit node.js自定义模块化

     

    Function walkFileTree()

    exports.walkFileTree =walkFileTree

     

     

    引用

     Files=require("./sdk/io/Files.js");

    console.log(dir)

    Files.walkFileTree(dir,function(path){

     

        console.log(path)

     })

     

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

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

    根据使用方法来说

    通常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 = sayHelloconsole.log(exports); 

    console.log(module.exports);

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

    var exports_mode = require('./exports_mode')

    exports_mode.sayHello()

    输出:

     


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

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


     终于讲清楚了nodejs中exports和module.exports的区别_慕课手记.mhtml

  • 相关阅读:
    bzoj3816 矩阵变换
    bzoj5029 贴小广告
    【BZOJ-1208】宠物收养所 Splay
    【BZOJ-2879】美食节 最小费用最大流 + 动态建图
    【BZOJ-1984】月下“毛景树” 树链剖分
    写在SDOI2016Round1前的To Do List
    BZOJ solve 100 纪念
    BZOJ-1143&&BZOJ-2718 祭祀river&&毕业旅行 最长反链(Floyed传递闭包+二分图匹配)
    【SDOI2009】解题汇总
    BZOJ-1879 Bill的挑战 状态压缩DP
  • 原文地址:https://www.cnblogs.com/attilax/p/15197046.html
Copyright © 2011-2022 走看看