首页 > node > node的模块系统

node的模块系统

作者:bin

封装模块demo:

//hello.js
module.exports = function () {
    var name;
    this.setName = function(thyName) {
        name = thyName;
    };
    this.sayHello = function() {
        console.log('Hello ' + name);
    };
};

当然也可以给方法命名,看你喜欢咯

function Helloo() {};
module.exports = Helloo;

模块使用:

var Hello = require('./he');
hello = new Hello();
hello.setName('nihao');
hello.sayHello();

您必须 [ 登录 ] 才能发表留言!