首页 > 代码库 > 学习nodejs -03

学习nodejs -03

使用模块

第一种应用:

//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.......‘);


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