nodejs调用模块

nodejs调用模块_第1张图片

function User(){
	this.id;
	this.name;
	this.age;
	this.enter=function(){
		console.log(this.name+'进入图书馆');
	}

}

module.exports = User;

nodejs调用模块_第2张图片

var http = require('http');

var User = require('./model/User');

http.createServer(function (request,response){
	response.writeHead(200, {'Contet-Type':'text/html;charset=utf-8'});
	
	if(request.url!=='/favicon.ico'){
		user = new User();
		user.id = 1;
		user.name = '张三';
		user.age=25;
		user.enter();
		response.end('');
	}	

}).listen(8000);

console.log('Server running at http://127.0.0.1:8000/');

function funl(res){
	console.log('fun1');
	res.write('hello ,我是fun1');
}

 

user还可以写构造方法

function User(id,name,age){
	this.id=id;
	this.name=name;
	this.age=age;
	this.enter=function(){
		console.log(this.name+'进入图书馆');
	}

}

module.exports = User;
var http = require('http');

var User = require('./model/User');

http.createServer(function (request,response){
	response.writeHead(200, {'Contet-Type':'text/html;charset=utf-8'});
	
	if(request.url!=='/favicon.ico'){
		user = new User(1,'张三',25);
		user.enter();
		response.end('');
	}	

}).listen(8000);

console.log('Server running at http://127.0.0.1:8000/');

function funl(res){
	console.log('fun1');
	res.write('hello ,我是fun1');
}

继承

nodejs调用模块_第3张图片

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

function Teacher(id,name,age){
	User.apply(this,[age,name,age]);
	this.teach=function(res){
		res.write(this.name+'剑客');
	}
}

module.exports = Teacher;
var http = require('http');

//var User = require('./model/User');
var Teacher = require('./model/Teacher');

http.createServer(function (request,response){
	response.writeHead(200, {'Contet-Type':'text/html;charset=utf-8'});
	
	if(request.url!=='/favicon.ico'){
		teacher = new Teacher(1,'张三',25);
		teacher.enter();
		teacher.teach(response);
		response.end('');
	}	

}).listen(8000);

console.log('Server running at http://127.0.0.1:8000/');

function funl(res){
	console.log('fun1');
	res.write('hello ,我是fun1');
}

你可能感兴趣的:(nodejs)