zoukankan      html  css  js  c++  java
  • [Node.js] Exporting Modules in Node

    In this lesson, you will learn the difference between the exports statement and module.exports. Two examples are demonstrated, each accomplishing the same task but one using export statements and one using module.exports. You will also learn the basic thumb rule to identify which is appropriate for your current needs.

    // circle.js using the exports statement
    var PI = Math.PI;
    
    exports.area = function(r){
      return PI * r * r;
    }
    
    exports.circumference = function(r){
      return 2 * Pi * r;
    }
    // accessing the exported functions in the node shell
    var circle = require('./circle.js');
    
    circle.area(4);
    circle.circumference(4);

    ---------------------

    // using module.exports to demonstrate the same functionality
    var PI = Math.PI;
    
    module.exports = function(r){
      return {
        area: function(){
          return PI * r * r;
        },
        circumference: function(){
          return 2 * PI * r;
        }
      }
    }
    // accessing the exposed functions in the node shell
    var circle = require('./circle.js');
    
    var myCircle = circle(4);
    
    myCircle.area();
    myCircle.circumference();

    To summarize that, the general thumb rule is use the exports statement to export instances of modules. Use the module.exports statement to export JavaScript objects.

  • 相关阅读:
    android一些细节问题
    Android Suspend/resume 过程分析.
    在NDK上建立自己的项目
    ListView加载特效
    Android Log Analysis转
    Android系统默认设置
    一步步分析Log
    Android Framework 分析
    编译安装MariaDB10.0.21
    mariadb多源复制 muiltil source replication
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5582631.html
Copyright © 2011-2022 走看看