学习nodejs -03

时间:2014-05-15 08:44:50   收藏:0   阅读:398

使用模块

第一种应用:

//module/AppUtils.js
exports.random = function () {
    return Math.random();
}
exports.showAuthor = function () {
    return "chenlong";
}
//app.js
var ut = require(‘./module/AppUtils‘);
console.info(‘.......start app.......‘);
var rn = ut.random();
var my = ut.showAuthor();
console.info(rn);
console.info(my);
console.info(‘.......end app.......‘);

第二种应用:

//module/User.js
var user = function () {
    var id;
    var name;
    var age;
}
user.prototype.show = function () {
    console.info(this.id + ‘:‘ + this.name + ‘:‘ + this.age);
}
module.exports = user;  //var u1 = new user();
//module.exports.user = user; //var u1 = new user.user();
   
//app.js
var user = require(‘./module/User‘);
console.info(‘.......start app.......‘);
var u1 = new user();
u1.id=1;
u1.name=‘chenlong‘;
u1.age = 23;
u1.show();
console.info(‘.......end app.......‘);


本文出自 “天空海阔” 博客,谢绝转载!

学习nodejs -03,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!