码迷,mamicode.com
首页 > Web开发 > 详细

Node.js 定义和引用模块

时间:2017-09-02 10:38:49      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:一个   结果   logs   his   turn   log   can   port   amount   

定义一个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

 

Node.js 定义和引用模块

标签:一个   结果   logs   his   turn   log   can   port   amount   

原文地址:http://www.cnblogs.com/sumuzhe/p/7465775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!