Node.js的模块

函数的形式

hello.js

exports.world = function() {
  console.log('Hello World');
}

main.js

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

类的形式

hello.js

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

main.js

var Hello = require('./hello'); 
hello = new Hello(); 
hello.setName('BYVoid'); 
hello.sayHello(); 
发布了290 篇原创文章 · 获赞 114 · 访问量 61万+

猜你喜欢

转载自blog.csdn.net/jason_cuijiahui/article/details/104179160