喔要做法令易讀器於是翻了JavaScript的文章,筆記一些如下:
可省略的參數 1 2 3 4 5 6 function abridgable (param1, param2 ) { if (typeof param2 == "undefined" ) param2 = "default value" ; return param1 + param2; } alert (abridgable ("haha " ));
變動引數數量 1 2 3 4 5 6 7 8 9 10 11 function foo ( ) { switch (arguments .length ) { case 0 : console .log ("There is no arguments." ); return ; case 1 : console .log ("There's only one argument." ); break ; default : console .log (arguments .length + " arguments." ); } var arg0 = arguments [0 ]; console .log ("The first argument is: " + arg0); }
把匿名函數指派給變數 1 2 3 4 anony = function ( ) { }
直接執行匿名參數 1 2 3 4 result = (function ( ) { })();
匿名函數的其他寫法 易混淆,不建議使用
1 2 3 4 5 6 (function ( ) { }()); result = function ( ){ }();
用匿名函數回傳函數 1 2 3 4 5 6 7 8 9 var arr = Array ();for (var i = 0 ; i < 10 ; ++i) { arr[i] = function ( ) { var tmp = i; return function ( ) { return tmp; } }(); }
若是直接return i; 那麼之後arr3 也只會得到10(即最後的i的值)
覆寫既存函數 以覆寫onLoad事件為例
1 2 3 4 5 6 if (window .onload ) old_onload = window .onload ;else old_onload = function ( ){};window .onload = function ( ) { old_onload (); alert ("頁面讀取完畢" ); }
用匿名函數覆寫既存函數 幫parseInt()加上基本的中文對照
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 parseInt = function ( ) { var orig = parseInt ; var chinese = "零一二三四五六七八九" ; return function ( ) { var result = orig.apply (this , arguments ); if (!isNaN (result)) return result; var str = arguments [0 ].replace (/\s/g , "" ); for (var i = 0 ; i < 10 ; ++i) { var re = new RegExp (chinese.charAt (i), "g" ); str = str.replace (re, String (i)); } return orig (str, 10 ); } }(); alert (parseInt ("七五三三九六七" ));
用JSON宣告物件 1 2 3 4 5 6 7 8 9 10 11 12 13 objA = { var1 : "public member variable" , var2 : "you can see it as attribute" , func1 : function ( ) { }, func2 : function ( ) { } }; objA.var1 = "other value" ; objA.func1 ();
用匿名函數來回傳物件 1 2 3 4 5 6 7 8 9 objB = function ( ) { alert ("objB" ); return { var1 : "public" , func1 : function ( ) { alert ("member function" ); } }; }();
不想用匿名函數的話當然也可以 1 2 3 4 5 6 7 8 9 10 11 ClassB = function ( ) { alert ("constructor" ); return { var1 : "public" , func1 : function ( ) { alert ("not constructor" ); } } } insB = ClassB (); insB2 = ClassB ();
以上應該夠用了,不過如果想要更多的話:
支援私有成員的物件 以「計數器」為例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 function ClassC ( ) { var counter = 0 ; var logger = function ( ) { console .log (counter); } return { name : "name" , inc : function ( ) { ++counter; logger (); return counter; } } } insC = ClassC (); alert (insC.inc ()); insC.counter ; insC.logger ; insC.logger (); insC.logger = "lalala" ; insC.inc ();
以上依舊可以用匿名函數的方式寫
1 2 3 4 5 6 7 8 9 10 11 12 13 objC = function ( ) { var counter = 0 ; var logger = function ( ) {console .log (counter);} return { name : "name" , inc : function ( ) { ++counter; logger (); return counter; } } }(); alert (objC.inc ());
用new宣告 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 function ClassD ( ) { var privateVar = "private" ; this .weirdVar = "weirdo" ; globalVar = "global" ; globalFunc = function ( ) { } this .weirdFunc = function ( ) { } var privateFunc1 = function ( ) { } function privateFunc2 ( ) { } } objD = ClassD (); insD = new ClassD ; insD2 = new ClassD ();
前述宣告方式如果有回傳值時,要特別小心
1 2 3 4 5 6 7 8 9 function ClassE ( ) { var privateVar = "private" ; var privateFunc = function ( ) { return "pFunc" ; } this .weirdVar = "weirdo" ; this .weirdFunc = function ( ) { return "wFunc" ; } return returnValue; } e1 = ClassE (); e2 = new ClassE ();
如果 returnValue 是函數、陣列、物件(null除外),那麼:
e1 與 e2 均等同 returnValue
weirdVar 和 weirdFunc 是全域
如果是物件,即同於前述 ClassC 的用法,可在 returnValue 中存取 privateVar 和 privateFunc
如果 returnValue 是數字(含NaN)、布林、字串、null、未定義(即直接return;
),那麼:
e1 是 returnValue,weirdVar 和 weirdFunc 消失無蹤
e2 是物件,weirdVar 和 weirdFunc 是公開成員
用 prototype 如果需要擴充既存實體,或是繼承其他類別,則需要此。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 function ClassF ( ){ alert ("constructor" ); } ClassF .prototype = { var1 : "public member variable" , var2 : "you can see it as attribute" , func1 : function ( ) { }, func2 : function ( ) { } } insF = new ClassF (); ClassF .prototype .addedFunc = function ( ) { alert ("new function" ); } insF.addedFunc ();
prototype 的應用範例 1 2 String .prototype .alert =function ( ) { alert (this ); }"application" .alert ();
Getter & Setter 例如Array元件的length表現的像個public member variable,但其實是個getter,不能直接覆寫。
1 2 3 4 5 6 7 8 9 10 11 function Field (val ){ var value = val; this .__defineGetter__ ("value" , function ( ){ return value; }); this .__defineSetter__ ("value" , function (val ){ value = val; }); }
參考資料