1.模块调用
node遵循AMD规范
//server.js var http = require("http"); var Teacher = require("./teacher"); http.createServer(function(request, response){ response.writeHead(200, {"Content-Type":"text/html; charset=uf-8"}); if (request.url!=="/favicon.ico") { let teacher = new Teacher(1, 'gaoxiong', 24); teacher.job(); response.end(); } }).listen(8000); console.log('Server running at http://127.0.0.1:8000/');
//user.js function User(id, name, age){ this.id = id; this.name = name; this.age = age; this.job = function(){ console.log('learning'); } } module.exports = User;
//teacher.js var User = require("./user"); function Teacher(id, name, age){ User.apply(this, [id, name, age]); this.job = function(){ console.log('teaching:'+this.name); } } module.exports = Teacher;