JavaScriptÅægíêéR[hÐɦÖÆ¢¤à̪ èÜ·B³¼Öð龵ĦÀs·é±ÆÅAubNXR[v̶ݵȢJavaScriptɨ¢Ä[IÉubNXR[vðÄ»µÜ·B
var a = "global"; (function(){ var a = "local"; alert(a); //local })(); alert(a); //global
êÔL¼ÈÌͱÌ(function(){ ... })()
Ì`®ÈÌÅ·ªAȺfunctionÌO¤ÉJbRªKvÈÌ©svcÉvÁ½±ÆÍȢŵ天H@½ßµÉfunction(){ ... }()
Æ¢ÄÝéÆASyntax Errorª¶µÜ·B
Ⱥfunction(){ ... }()ÍSyntax ErrorÈÌ©
JavaScriptÉÍfunction¶Æfunction®ª ÁÄA÷É®ªá¢Ü·B\ɢĵܤÆA"function"Æ¢¤L[[h©çnÜéæ¤ÉƶÉÈèA»êÈOÌ«ûð·éÆ®ÉÈèÜ·B
á¦ÎºÌæ¤È«ûÌÆ«AZ~Rð©ÈÄ஫ܷæËH@±êÍJavaScriptÌnªAfunction¶Å éÆF¯µÄAu}v̼ãŧIɶðæØé©çÅ·B¼ãÉJbRª ÁÄàA³êܹñB
function hoge() { alert("hoge"); } // ±±ÉZ~RðKvÍÈ¢I (a == b) ? alert('a==b') : alert('a!=b') ;
±êͳ¼ÖÅ௶ÈÌÅAfunction(){ ... }()
ÆÆAnÌàÅÍȺÌæ¤Éðß³êĵܢܷB±ÌÆ«A();
ÍJavaScriptƵÄs³È\¶ÈÌÅA±±ÅG[ª¶·éÌÅ·B
function(){ ... }; (); // <-Syntax Error!
±Ì®ðÅ¿Á·ÉÍfunction®ÆµÄKvª èÜ·B¦¿functionÈO©ç«nßê΢¢ÌÅA±Ì½ßÉJbRðgÁÄ¢éí¯Å·B
JbRÈOÌ«ûðl¦é
tÉl¦éÆAfunction®ÆµÄF¯³¹çêé\¶Å êÎAÊÉJbRðgíÈÄà¦Öª¯é±ÆÉÈèÜ·B±±Åg¦éàÌA»êÍPZqÅ·B
JavaScript GardenÉPÁZZqu{vðgÁ½áªoĢܷªAJavaScriptÌPZqƾ¦Îu+vu-vu!vuvoidvutypeofvª èÜ·ÌÅA±êçàg¤±ÆªÅ«Ü·B
var a = "global"; (function(){ var a = "another paren"; alert(a); //"another paren" }()); +function(){ var a = "plus"; alert(a); //plus }(); -function(){ var a = "minus"; alert(a); //minus }(); !function(){ var a = "ex"; alert(a); //ex }(); void function(){ var a = "void"; alert(a); //void }(); typeof function(){ var a = "typeof"; alert(a); //typeof }();
±êAÈñÆS®«Ü·@ʢŷæËBu+vÍu()vÉä×Ä1byteȪūéÌÅAZ«½¢Æ«Ég¦éc©ÈH
newÍá±®ìªá¤ÌÅÓªKv
ÖÀèÅg¦éZqÅ éunewvàPZqÌêíÅ·ªAXR[vàÅthiș·à̪ÏíÁÄéÌŵӪKvÅ·BܽAIuWFNgÌú»ª¶·é½ßÉA»Ì¼Ì«ûÉä×êεptH[}XªòèÜ·Bi½ñà[vµÈ¢ÀèÌ´Å«È¢xŵ夯Çj
newÉÍøðgíÈ¢êÍJbRðȪūéÆ¢¤Ê¢«¿ª èÜ·B±êðp·éÆnew function(){ ... }
Æ¢¤«ûª¶ÜêÜ·Bá±ÇÝâ·¢©àµêܹñB
new function(){ var a = "new"; alert(a); //new };
´
- voidªÓOÆÇÝâ·¢B
- Ó¡IE¶¬IÉ©RB
- voidÍg¢Ç±ëªÙÆñÇÈ¢ÌÅA¦ÖÉgÁÄ¢êÎ\[XR[hÌõªµâ·Èé
- N×µÄàAܾ¾êIÈ©ª©Â©é̪JavaScriptB
¦u¦ÖvÆ¢¤¼OÍJavaScriptp^[Åo¦Üµ½B
O̽ßÇL
´³¦í©ÁĵܦμÉà¢çÅà«ûª é±Æªí©èÜ·BzñRXgN^Ì[]ðgÁÄ[function(){c}()]
ƵÄࢢŷµAãü·éêÍ»à»àÁÊÈ«ûÍKvÈAvar hoge = function(){c}();
Æ»ÌÜܱƪūܷB
½¾A«ûÉæÁÄÍßèlªÏíÁĵܤ±ÆÍo¦Ä¨©È¯êÎÈèܹñB
JbRðp¢½êÍßèlÉÏ»ª èܹñªAu+vu-vÍßèlðnumber^ÉLXgµAu!vÍbool^ÉLXgµAuvoidvÉÁÄÍíÉuundefinedvðÔ·æ¤ÉÈèÜ·B
res = (function(){return "1"})(); //stringÌ"1"i³ÌÜÜj res = +function(){return "1"}(); //numberÌ1 res = -function(){return "1"}(); //numberÌ-1 res = !function(){return "1"}(); //false res = void function(){return "1"}(); //undefined
ÂçÂç¢Ä«Üµ½ªÇÌƱëA(functiton(){c})()ªÅàÄp«ªAptH[}XÉàDêA»±»±ÌÂÇ«àÛÂÅåöñ¾Æ¾¦éñ¶áȢŵ天BißÁīĵܢܵ½cj
N¾©mèܹñªAÅɱÌL@ðv¢Â¢½lÍÌåÅ·B
keyword: javascript