-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_test.js
52 lines (45 loc) · 1.06 KB
/
module_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function StringBuilder(){
var buffer = [];
this.add = function(str){
buffer.push(str);
}
this.toString = function(){
return buffer.join('');
}
}
var sb = new StringBuilder();
console.log(sb);//每个实例的add/toString都不同,每个实例一份拷贝
function StringBuilder2(){
this._buffer = [];
}
StringBuilder2.prototype = {
constructor:StringBuilder2,
add:function(str){
this._buffer.push(str);
},
toString:function(){
return this._buffer.join('');
}
};
var sb2= new StringBuilder2();
console.log(sb2);//私有变量放到实例对象,可以从外部读取
sb2.add('test');
console.log(sb2.toLocaleString());
var module1 = (function(){
var _buffer = [];
var add = function(str){
_buffer.push(str);
};
var toString = function(){
return _buffer.join('');
};
return {
add : add,
toString: toString
};
})();
console.log(module1);
console.log(module1._buffer);
module1.add('module');
module1.add(' test');
console.log(module1.toString());