Skip to content

Commit b7dc162

Browse files
author
zhangshouyong
committed
简单的单例
1 parent fd50e01 commit b7dc162

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

server.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var http =require('http');
2+
http.createServer(function(request,response){
3+
response.writeHead(200,{'Content-Type':'text/plain'});
4+
response.end('hello node.js\n');
5+
}).listen(8080);
6+
console.log("server running at 8080");

singleton.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var singleton=function(name){
2+
this.name=name;
3+
this.instance=null;
4+
}
5+
singleton.prototype.getName=function(){
6+
alert(this.name);
7+
}
8+
singleton.getInstance= function(name){
9+
if(!this.instance){
10+
this.instance= new singleton(name);
11+
}
12+
return this.instance;
13+
}
14+

0 commit comments

Comments
 (0)