您的位置:首页 > Web前端 > Node.js

nodejs 加载模块

2016-03-25 19:47 465 查看
function hello(){
var name;
this.setName = function(n){
name = n;
}
this.sayHello = function(){
console.log('Hello ' + name);
}
}
module.exports = hello;

加载的是类型

var hello = require('./hello');

var h = new hello();
h.setName('xiuye');
h.sayHello();
var a = {a:1,b:2,c:3};

module.exports = a;


加载的是对象实例
xiuye@ubuntu:~/workspace$ nodejs
> require("./test");
{ a: 1, b: 2, c: 3 }
> a = require("./test");
{ a: 1, b: 2, c: 3 }
> a
{ a: 1, b: 2, c: 3 }
> a.a
1
> a.b
2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: