網站製作學習誌

記錄學習製作網站的一切

JavaScript 的靜態變數與靜態方法

之前提到 JavaScript 撰寫物件時的寫法:

1
2
3
4
5
6
7
8
9
10
11
12
物件類別名稱 = function([參數]) {
this.屬性1 = null;
this.屬性2 = new Array();
this.方法1 = function([參數]) {
<font color="#009900">// 方法1程式碼</font>
this.屬性1 = 'DEF';
}
this.方法2 = function([參數]) {
<font color="#009900">// 方法2程式碼</font>
this.屬性2 = new Array(4, 5, 6);
}
}

這種方法在有物件實體時才會有作用,如果要像 Java 或 PHP 一樣呼叫靜態方法的話,就要這樣寫:

1
2
3
4
5
6
7
8
物件類別名稱 = function([參數]) {
}
<font color="#009900">// 一定要在建構函式之後再宣告,否則會有錯誤。</font>
物件類別名稱.靜態變數 = null;
物件類別名稱.靜態方法 = function([參數]) {
<font color="#009900">// 這時候不可使用 this 關鍵字</font>
物件類別名稱.靜態變數1 = 'DEF';
}

這樣我們可以利用它來建立一個 Singleton 類別 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
MyTest = function() {
this.msg = null;
this.echo = function() {
alert(this.msg);
}
this.setMsg = function(msg) {
this.msg = msg;
}
}
<font color="#009900">// 這裡是關鍵</font>
MyTest._instance = null;
MyTest.getInstance = function() {
if (!MyTest._instance)
{
MyTest._instance = new MyTest();
this.msg = 'test!';
}
return MyTest._instance;
}

測試如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
<script type="text/javascript" src="test.js"></script>
<script type="text/javascript">
<!--
<font color="#FF0000"><strong>var test1 = MyTest.getInstance();</strong></font>
test1.echo(); <font color="#009900">// show 'null'</font>
test1.setMsg('this is test1!');
test1.echo(); <font color="#009900">// show 'this is test1!'</font>
<font color="#FF0000"><strong>var test2 = MyTest.getInstance();</strong></font>
test2.echo(); <font color="#009900">// show 'this is test1!'</font>
test2.setMsg('this is test2!');
<strong>test1</strong>.echo(); <font color="#009900">// show 'this is test2!'</font>
//-->
</script>

可以看得出來, test1 和 test2 其實都是指向 MyTest._instance 。

當然用 JavaScript 實作 Singleton 的實際意義不大 (建構式還是公開的) ,這裡我只是在強調 JavaScript 靜態方法及變數的寫法而已。

Comments