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

  • 相关阅读:
    正则表达式简介
    每个程序员都必须遵守的编程原则
    String, StringBuffer, StringBuilder
    一些软件设计的原则
    A hard puzzle ACM HDU1097
    ACM HDU 1032The 3n + 1 problem
    Humble Numbers HDU 1058 ACM
    ACM HDU 1028Ignatius and the Princess III
    HangOver ACM HDU1056
    ACM HDU 1021Fibonacci Again
  • 原文地址:https://www.cnblogs.com/attilax/p/15197046.html
Copyright © 2011-2022 走看看