zoukankan      html  css  js  c++  java
  • Node.js 定义和引用模块

    定义一个Node模块:currency.js

    var canadianDollar = 0.91;
    
    function roundTwoDecimals(amount) {
        return Math.round(amount * 100) / 100;
    }
    
    exports.canadianToUS = function (canadian) {
        return roundTwoDecimals(canadian * canadianDollar);
    }
    
    exports.USToCanadian = function (us) {
        return roundTwoDecimals(us / canadianDollar);
    }

    引入一个模块:

    下面的test-currency.js中的代码,它require了currency.js模块:

    var currency = require('./currency');
    
    console.log('50 Canadian dollars equals this amount of US dollars:');
    console.log(currency.canadianToUS(50));
    
    console.log('30 US dollars equals this amount of Canadian dollars:');
    console.log(currency.USToCanadian(30));

    输出结果:

    50 Canadian dollars equals this amount of US dollars:
    45.5
    30 US dollars equals this amount of Canadian dollars:
    32.97
  • 相关阅读:
    Many Equal Substrings CF
    Seek the Name, Seek the Fame POJ
    人人都是好朋友(离散化 + 并查集)
    建设道路
    day_30
    day_29作业
    day_29
    day_28
    day_27
    day_26作业
  • 原文地址:https://www.cnblogs.com/sumuzhe/p/7465775.html
Copyright © 2011-2022 走看看