前言
JavaScript并不像別的語言,能使用關鍵字來聲明私有變量。
我了解的JavaScript能用來聲明私有變量的方式有兩種,一種是使用閉包,一種是使用WeakMap。
閉包
閉包的描述有很多種,比如:
能訪問其它函數作用域的函數;
內部函數訪問外部函數作用域的橋梁;
......
使用閉包構建私有變量的邏輯在于:
1.在外部函數中聲明變量和內部函數;
2.使用內部函數訪問或者修改變量值;
3.在外部函數內返回內部函數;
1
2
3
4
5
6
7
8
|
function outside(){ let val = 123; function inside(){ return val; } return inside; } console.log(outside()()); //123 |
通過我上面的例子能夠大致了解使用閉包構建私有變量的邏輯,但是不足以體現私有變量的重要性,一個const變量也能達到上述代碼的效果:
1
2
3
|
//同樣的能訪問,但是不能修改,達到了上述代碼的效果 const val = 123; console.log(val); //123 |
接下來的代碼,將具體體現私有變量的重要性:
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
function person(){ let _name = 'unknown' ; let _age = 18; let _sex = 'man' ; function setName(name){ _name = name || 'unknown' ; } function getName(){ return _name; } function setAge(age){ if ( typeof age === 'number' ){ _age = Math.floor(age); } else { throw Error( "typeof age !== 'number'" ); } } function getAge(){ return _age; } function setSex(sex){ if (sex === 'man' || sex === 1){ _sex = 'man' ; } else if (sex === 'woman' || sex === 0){ _sex = 'woman' ; } else { throw Error( 'input error' ); } } function getSex(){ return _sex; } return { setName : setName, getName : getName, setAge : setAge, getAge : getAge, setSex : setSex, getSex : getSex } } let xiaoming = person(); let xiaohong = person(); xiaoming.setName( 'xiaoming' ); xiaohong.setName( 'xiaohong' ); console.log( 'xiaoming name : ' + xiaoming.getName()); //xiaoming name : xiaoming console.log( 'xiaohong name : ' + xiaohong.getName()); //xiaohong name : xiaohong xiaoming.setAge(19.3333); xiaohong.setAge( '16' ); //Uncaught Error: typeof age !== 'number' console.log( 'xiaoming age : ' + xiaoming.getAge()); //xiaoming age : 19 console.log( 'xiaohong age : ' + xiaohong.getAge()); //xiaohong age : 18 xiaoming.setSex(1); xiaohong.setSex( 'woman' ); console.log( 'xiaoming sex : ' + xiaoming.getSex()); //xiaoming sex : man console.log( 'xiaohong sex : ' + xiaohong.getSex()); //xiaohong sex : woman |
從上面的代碼中,可以看出,如果想要設置或者獲取 _name、_age、_sex三個變量的值,只能通過固定的 setName、getName、setAge、getAge、setSex、getSex等方法,而在所有的setter方法中,都對形參進行了判斷。也就意味著,對對象的所有操作都將在掌控之中,這在某一層面上弱化了JavaScript作為弱類型語言上的一些負面影響。
WeakMap
如果對WeakMap不是很了解的可以先看WeakMap的詳細介紹。
這里主要是利用WeakMap的key不可枚舉這一知識點。
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
let nameWeakMap = new WeakMap(); let ageWeakMap = new WeakMap(); let sexWeakMap = new WeakMap(); function person(){ let _hash = Object.create( null ); nameWeakMap.set(_hash, 'unknown' ); ageWeakMap.set(_hash,18); sexWeakMap.set(_hash, 'man' ); function setName(name){ nameWeakMap.set(_hash,name || 'unknown' ); } function getName(){ return nameWeakMap.get(_hash); } function setAge(age){ if ( typeof age === 'number' ){ ageWeakMap.set(_hash,Math.floor(age)); } else { throw Error( "typeof age !== 'number'" ); } } function getAge(){ return ageWeakMap.get(_hash); } function setSex(sex){ if (sex === 'man' || sex === 1){ sexWeakMap.set(_hash, 'man' ); } else if (sex === 'woman' || sex === 0){ sexWeakMap.set(_hash, 'woman' ); } else { throw Error( 'input error' ); } } function getSex(){ return sexWeakMap.get(_hash); } return { setName : setName, getName : getName, setAge : setAge, getAge : getAge, setSex : setSex, getSex : getSex } } let xiaoming = person(); let xiaohong = person(); xiaoming.setName( 'xiaoming' ); xiaohong.setName( 'xiaohong' ); console.log( 'xiaoming name : ' + xiaoming.getName()); //xiaoming name : xiaoming console.log( 'xiaohong name : ' + xiaohong.getName()); //xiaohong name : xiaohong xiaoming.setAge(19.3333); xiaohong.setAge( '16' ); //Uncaught Error: typeof age !== 'number' console.log( 'xiaoming age : ' + xiaoming.getAge()); //xiaoming age : 19 console.log( 'xiaohong age : ' + xiaohong.getAge()); //xiaohong age : 18 xiaoming.setSex(1); xiaohong.setSex( 'woman' ); console.log( 'xiaoming sex : ' + xiaoming.getSex()); //xiaoming sex : man console.log( 'xiaohong sex : ' + xiaohong.getSex()); //xiaohong sex : woman |
同樣達成了構建私有變量的效果。順便提一句,class中構建私有變量用的就是WeakMap。
結尾
這篇文章只是記錄我知道的關于JavaScript構建私有變量的方法以及作用,如有錯誤和遺漏,歡迎指出,不勝感謝。
以上就是JavaScript 聲明私有變量的兩種方式的詳細內容,更多關于JavaScript 聲明私有變量的資料請關注服務器之家其它相關文章!
原文鏈接:https://www.cnblogs.com/liujingjiu/p/14377246.html