ééã1-ã°ããŒãã«å€æ°ã®äœ¿çš
JavaScriptã«ãã粟éããŠããªãã®ã§ããã°ããã¹ãŠã®å€æ°ãã°ããŒãã«ã§ãããšãã«ãããçŽ æŽããããšæãã§ãããã å®éããã®ããŒã«ã®ãã¹ãŠã®è€éããç¥ã£ãŠãããšã¯éããŸããã ã°ããŒãã«å€æ°ã¯ãå¥ã®.jsãã¡ã€ã«ã«ããŒããããŠããå Žåã§ããã³ãŒãã®ã©ã®éšåããã§ãã¢ã¯ã»ã¹ã§ããå€æ°ã§ãã é åçã§ããã ãã¹ãŠã®å€æ°ã¯åžžã«å€æŽå¯èœã§ãã
å®éããããã
æå³ããã«å€ãäžæžãããå¯èœæ§ããããããããã¯æªãèãã§ãã ããªãããªã³ã©ã€ã³ã¹ãã¢ãæã£ãŠããŠãJavaScriptã䜿çšããŠãã¹ã±ããã«è¿œå ãããã¢ã€ãã ã®äŸ¡æ Œã®åèšãèšç®ãããšããŸãã ãµã³ãã«ã³ãŒãã次ã«ç€ºããŸãã
var total = 0, // tax = 0.05; // 5%
ã³ãŒãã䜿çšããŠããŒãžã«ãã€ãŒãã衚瀺ãããã補åã®ããã®ã£ã©ãªãŒãäœæããããããšããŸãã ãããŠã次ã®ãããªã³ãŒããå«ãŸããŠããå¯èœæ§ããããŸãã
var total = 15; // - twitter
ãŸãã¯
var tax = function () { /* ... */ }; //
ããã§åé¡ãçºçããŸããã 2ã€ã®éèŠãªå€æ°ãäžæžããããã³ãŒããé©åã«æ©èœããŠããŸããã ãããæ¯æãããšã¯ãæžãæãã«è²»ãããã貎éãªæéã§ãã
ããã§è§£æ±ºçã¯äœã§ããïŒ ã€ãŸãã ã«ãã»ã«å ; ãããããããåé¿ããæ¹æ³ã¯ä»ã«ããããããããŸãã æåã«ãåŒã³åºãèªäœãå¿åé¢æ°ã®åœ¢åŒã§ãã¹ãŠã®ã³ãŒããèšè¿°ã§ããŸãã
(function () { var total = 0, tax = 0.05; // - }());
ãããŠãå€éšããã®ã³ãŒãã¯é¢æ°å ã®ã³ãŒãã«å°éããŸããã ããã¯ãå人çšãã³ãŒãã«ã¯é©ããŠããŸãããå¿ èŠãªæ©èœãæäŸããŸããã ããšãã°ãä»ã®äººããã³ãã¬ãŒããšããŠäœ¿çšã§ãããã¹ã±ããã«ãŠã³ã¿ãŒãå¿ èŠãªå Žåã¯ã次ã®ããã«ãããšããã§ãããã
var cartTotaler = (function () { var total = 0; tax = 0.05; // return { addItem : function (item) { }, removeItem : function (item) { }, calculateTitle : function () { } }; }());
ã°ããŒãã«å€æ°ã«ã€ããŠããå°ãïŒå€æ°ã®äœææã«varããŒã¯ãŒãã䜿çšããªãå ŽåãJavaScriptãšã³ãžã³ã¯ããã©ã«ãã§ã°ããŒãã«ãšããŠå®çŸ©ããããšã«æ³šæããŠãã ããã äŸïŒ
(function () { tax = 0.05; }()); var totalPrice = 100 + (100 * tax); // 105
çšå€æ°ã¯é¢æ°ã®å€éšã§äœ¿çšã§ããŸããããã¯ã宣èšæã«varãšããåèªã䜿çšãããªãã£ãããã§ãã ããã«æ³šæããŠãã ããã
ãšã©ãŒ2-ã»ãã³ãã³ãå¿ãã
JavaScriptã®ã¹ããŒãã¡ã³ãã¯ã»ãã³ãã³ã§çµããå¿ èŠããããŸãã ãšãŠãç°¡åã§ãã å¿ããŠããŸã£ãå Žåãã³ã³ãã€ã©ãããªãã®ããã«ä»äºãããŸãã ã ãããããªãã¯ãããã眮ãããšã¯ã§ããŸãããïŒ
ãŸããããã€ãã®å Žæã§ããã¯éèŠã§ãã ããšãã°ãã«ãŒãæ¬äœã§è€æ°ã®ã¹ããŒãã¡ã³ããå®è¡ããŠããå Žåã ããããªããšãã³ã³ãã€ã©ãŒã¯ãšã©ãŒãã¹ããŒããŸãã è¡ã®çµããã¯ã©ãã§ããïŒ
JavaScriptã³ãã¥ããã£ã¯ããã«åãããŠããŸãã ããªã±ãŒãã®äž¡åŽã«ã¯åŒ·ãè°è«ããããŸãã ç§ã®äž»åŒµã¯æ¬¡ã®ãšããã§ãããã®è³ªåã§ã³ã³ãã€ã©ã«äŸåããŠããå ŽåïŒæãåçŽãªã³ãŒãã§ãã£ãŠãïŒãç«ã§éãã§ããŸãã
ããšãã°ã次ã®ãããªåçŽãªé¢æ°ïŒ
function returnPerson (name) { return { name : name }; }
ããã¯çŽ æµãªå°ããªãªããžã§ã¯ããè¿ãã¯ãã§ã...ããããã³ã³ãã€ã©ã¯ããªã¿ãŒã³åŸã«ã»ãã³ãã³ãå ¥ããããšå€æããã®ã§ãäœãè¿ãããããªããžã§ã¯ãã¯ç¡èŠãããŸãã 次ã®ããšãè¡ãå¿ èŠããããŸãã
return { name : name };
ãããã£ãŠãããªããžã®ç§ã®ã¢ããã€ã¹ã¯ã»ãã³ãã³ã眮ãããšã§ãã æ£çŽãªãšãããããã¯ããªãæ©ãç¿æ £ã«ãªããŸãã ãã ããWebéçºè ãšããŠã¯ãããããã»ãã³ãã³ãå ¥ããå¿ èŠã®ãªãä»ã®èšèªïŒPHPãªã©ïŒã䜿çšããŸãã ãããªããªãïŒ
泚ïŒäžããããšãèš±å¯ãããŠããåç¶æ³ãããããªãå Žåã¯ããªã¹ã¯ãåããªãæ¹ãè¯ãã§ãããã
ééã3-==ã®äœ¿çš
æåã®JavaScriptéçºè ã«ãJavaScriptã§æãäžè¬çãªãšã©ãŒã¯äœã§ããïŒããšå°ãããšãã»ãšãã©ã®å Žåãã===ã®ä»£ããã«==ã䜿çšããšçããŸãã ããã¯ã©ãããæå³ã§ããïŒ
ãããè©ŠããŠãã ããïŒ
if (1 == 1) { console.log("!"); }
ã³ãŒããæ©èœããããšãæåŸ ããŠããŸãããïŒ ã§ã¯ã次ãè©ŠããŠãã ããã
if (1 == '1') { console.log("!"); }
ã¯ãããçå®ïŒãã³ã³ãœãŒã«ã«ãããã¢ãã衚瀺ãããŸã...ãããŠãããã¯æªãããšã§ãã ããã§ã¯æ¬¡ã®ããšãè¡ãããŸãã==ã¯çåæŒç®åã§ãã 2ã€ã®å€æ°ãå¯èœãªéãé¡äŒŒãããŸãã ãã®å Žåãæååã1ãã¯æ°å€1ã«å€æãããif-statementã¯trueãè¿ããŸã ã
ããªããç解ããŠããããã«ã解決çã¯===;ã䜿çšããããšã§ãã ããã¯ãã¹ãŠãæŒç®åã®å Žåã«æå¹ã§ãïŒ==ããã³ïŒ=ã
ãããŠä»ã楜ãã¿ã®ããã«ãäºéã®å¹³çããçãŸããããã€ãã®æé«ã®ééãïŒ
'' == '0' // false '0' == '' // true false == '0' // true ' \t\r\n ' == 0 // true
ééã4-ã¿ã€ããã©ãããããªããžã§ã¯ãã®äœ¿çš
JavaScriptïŒïŒïŒèŠªåã«ïŒïŒïŒåãç°¡åã«äœæããããã®åã©ãããŒãæäŸããŸãã
new Number(10); new String("!"); new Boolean(true); new Object(); new Array("", "", "");
ãŸããéåžžã«äžäŸ¿ã§ãã ããã¯ãã¹ãŠãéåžžã«å°ãªãæ°ã®ããŒã¹ãããŒã¯ã§å®è¡ã§ããŸãã
10; ""; true; {}; ["", "", ""];
ããããããã ãã§ã¯ãããŸããããããã®2ã€ã®ããšã¯ãŸã£ããåãã§ã¯ãããŸããã ãã°ã©ã¹ã»ã¯ããã¯ãã©ãŒã*ããã®äž»é¡ã«ã€ããŠèšã£ãŠããããšã¯æ¬¡ã®ãšããã§ãã
ããšãã°ã æ°ããããŒã«å€ïŒfalseïŒã¯ãã©ãããããå€ãè¿ãvalueOfã¡ãœãããæã€ãªããžã§ã¯ããçæããŸãã
-JavaScriptïŒè¯ãéšåã114ããŒãž
ããã¯ã typeof new NumberïŒ10ïŒãŸãã¯typeof new StringïŒ "HelloïŒ"ïŒãå®è¡ãããšãããªããžã§ã¯ãããåŸãããããšãæå³ããŸã-æãã ãã®ã§ã¯ãããŸããã ããã«ãã·ã§ã«ã䜿çšãããšãäºæããªãåå¿ãçºçããå¯èœæ§ããããŸãã
ã§ã¯ããªãJavaScriptããããã®ãªããžã§ã¯ããæäŸããã®ã§ããããïŒ å éšäœ¿çšã ããªããã£ãå€ã«ã¯ã¡ãœããããããŸããïŒãªããžã§ã¯ãã§ã¯ãªãããïŒã ãã®ãããããªããã£ããªããžã§ã¯ãã§ã¡ãœãããåŒã³åºããããšïŒããšãã°ã "Hello peopleïŒ" replaceïŒ "people"ã "habralyudi"ïŒïŒãJavaScriptã¯æååã®ã©ãããŒãäœæãããžã§ããå®è¡ããŠãããªããžã§ã¯ããç Žæ£ããŸãã
JavaScriptã®ã©ãããŒããã®ãŸãŸã«ããŠãããªããã£ãå€ã䜿çšããŸãã
ãšã©ãŒ5-For-In䜿çšæã®å±æ§æ€èšŒãªã
ç§ãã¡ã¯ãã¹ãŠé åã®åæã«ç²ŸéããŠããŸãã ãã ãããªããžã§ã¯ãã®ããããã£ãå埩åŠçããå¿ èŠãããå ŽåããããŸãã ïŒäœè«ãå®éãé åã®èŠçŽ ã¯ã1ã€ã®ãªããžã§ã¯ãã®çªå·ä»ãããããã£ã§ããïŒããããŸã è¡ã£ãŠããªãå Žåã¯ãFor-Inã«ãŒãã䜿çšããŸããã
var prop, obj = { name: "", job: "", age: 55 }; for (var prop in obj) { console.log(prop + ": " + obj[prop]); }
ãã®ã³ãŒããå®è¡ãããšã次ã®åºåã衚瀺ãããŸãã
name: job: age: 55
ãã ãããã©ãŠã¶ã«ã¯ããã§ãŒã³ã®ããã«äžã«ããããã£ãšã¡ãœãããå«ãŸããŸãã ã»ãšãã©ã®å Žåããããã®ããããã£ãäžèŠ§è¡šç€ºããããªãã§ãããã hasOwnPropertiesã䜿çšããŠããªããžã§ã¯ãã§ã¯ãªãããããã£ãé€å€ããå¿ èŠããããŸãã
Function Dog (name) { this.name = name; } Dog.prototype.legs = 4; Dog.prototype.speak = function () { return "!"; }; var d = new Dog(""); for (var prop in d) { console.log( prop + ": " + d[prop] ); } console.log("====="); for (var prop in d) { if (d.hasOwnProperty(prop)) { console.log( prop + ": " + d[prop] ); } } // Output // name: // legs: 4 // speak: function () { return "!"; // } // ===== // name:
ããããã£ãå¿ èŠãªå ŽåããããŸãããããã€ãã®ã¡ãœãããé€å€ãããå ŽåããããŸãã ããã¯typeofã䜿çšããŠå®è¡ã§ããŸãã
for (var prop in d) { if (typeof d[prop] !== 'function') { console.log( prop + ": " + d[prop] ); } }
ãããã«ãããäžèŠãªçµæãé¿ããããã«ãfor-inåŒã¯åžžã«æ確ã§ãã
ãšã©ãŒ6- withãŸãã¯evalã®äœ¿çš
幞ããªããšã«ãä»æ¥ã®ã»ãšãã©ã®æ å ±æºã¯evalã§åŠç¿ããŠããŸããã ãã ããå€ããœãŒã¹ãŸãã¯ããŸãä¿¡é Œã§ããªããœãŒã¹ã䜿çšããå ŽåïŒã€ã³ã¿ãŒãããäžã§åªããè³æãèŠã€ããã®ã¯éåžžã«å°é£ãªå ŽåããããŸãïŒã ããã§æ€çŽ¢ãŸãã¯è©äŸ¡ããŠäœ¿çšã§ããŸãã ã²ã©ãã¹ã¿ãŒããéçºè ã
ããã§ã¯ãå§ããŸãããã 䜿çšã§ããªã2ã€ã®äž»ãªçç±ïŒ
- ã³ãŒããé ããªããŸãã
- ããªããäœãããŠããã®ãã¯å¿ ãããæ確ã§ã¯ãããŸããã
ãã€ã³ã1ã¯å°é¢ã«ç«ã€ã ããã§ã¯ãå°ãèŠãŠã¿ãŸãããã withã®ä»çµã¿ã¯æ¬¡ã®ãšããã§ãããªããžã§ã¯ããwith-expressionã«éä¿¡ããŸãã 次ã«ãwithãããã¯å ã§ãå€æ°ãšããŠãªããžã§ã¯ãã®ããããã£ã«ã¢ã¯ã»ã¹ã§ããŸãã
var person = { name: "", age : 10 }; with (person) { console.log(name); // console.log(age); // 10 }
ããããwithãªããžã§ã¯ãã®ããããã£ãšåãååã®å€æ°ãããå Žåã¯ã©ãã§ããããïŒ åºæ¬çã«ãäž¡æ¹ãããå Žåãå€æ°ã䜿çšãããŸãã ãã ããwithå ã®ãªããžã§ã¯ãã«ããããã£ãè¿œå ããããšã¯ã§ããŸããã ããããã£ããªãå ŽåããŸãã¯å€æ°ãååšããå Žåãå€æ°ã¯withã¹ããŒãã¡ã³ãã®å€éšãããããå®è¡ã§ããŸãã
var person = { name: "", age : 10 }, name = ""; with (person) { console.log(name); // job = ""; } console.log(person.job); // ; console.log(job); //
evalã«ç§»ããŸãããã ç°¡åã«èšãã°ãã³ãŒãè¡ãé¢æ°ã«æž¡ããšããããå®è¡ãããŸãã
eval( "Console.log('!');" );
ç¡å®³ã«èãããŸãããããã«åŒ·åã«èãããŸããïŒ å®éããããäž»ãªåé¡ã§ã-ãã¯ãŒã倧ããããŸãã æããã«ããã®ãããªè¡ãæžãçç±ã¯ãããŸããããªããªãã1ïŒãªãç°¡åãªæ¹æ³ã§æžããªãã®ãïŒ ãããŠã2ïŒevalã¯åæ§ã«ã¹ããŒããŠã³ããŸãã ãããã£ãŠãevalã®äž»ãªçšéã¯ãçŸæç¹ã§ã¯å¿ èŠã®ãªãã³ãŒããå®è¡ããããšã§ãã ãµãŒããŒããããŸãã¯ãŠãŒã¶ãŒããçŽæ¥åãåãããšãã§ããŸãã ãµã€ããŠãŒã¶ãŒã«ã³ãŒãã®å®å šãªå¶åŸ¡ãèš±å¯ããŠãããããã§ããïŒ ããªãããšãæã¿ãŸãã ããã«ã圌ã¯ããã«ãŒã®ããã«åœŒã®ãŠã§ããµã€ããéããŸããEVALã®äœ¿çšã¯ããç§ã¯é¢ããŠããŠãéµã¯æ·ç©ã®äžã«ãããŸãããšãããµã€ã³ã§ããããªãèªèº«ãŸãã¯ãŠãŒã¶ãŒãæããŠãããªããããã䜿ããªãã§ãã ããã
ãšã©ãŒ7- parseIntã䜿çšããéã«çªå·ã·ã¹ãã ã䜿çšããããšãå¿ãã
JavaScriptã¯ãæ°åãå«ãæååãæ°åã«å€æããã®ã«åœ¹ç«ã€å°ããªé¢æ°ãæäŸããŸãã
parseInt("200"); // 200 parseInt("043"); // 35
ãããŠãããã§äœãèµ·ãã£ãã®ã§ããïŒ 2çªç®ã®äŸã¯43ã§ãã£ãŠã¯ãªããŸãããïŒ å®éã parseIntã¯10é²ã·ã¹ãã ã§ã®ã¿æ©èœããŸããã 0ã§å§ãŸãè¡ãèŠããšã圌ã¯ããã8é²æ°ãšèŠãªããŸãã ãããã£ãŠãçªå·äœç³»ãå¿ããŠã¯ãªããŸããã 圌ãã¯åºæ°ã«ã€ããŠé¢æ°ã話ããŸãã
parseInt("020", 10); // 20 parseInt("100", 2); // 4
ãšã©ãŒ8- ifããã³while ã¹ããŒãã¡ã³ãã䜿çšããå Žå ãæ¬åŒ§ã¯äœ¿çšãããŸãã
JavaScriptã®ã¡ãªããã®1ã€ã¯æè»æ§ã§ãã ããããæã«ã¯åœŒå¥³ã¯ããªãã«èãåããããšãã§ããŸãã ããã¯ãè€åifããã³while ã¹ããŒãã¡ã³ãã®äžæ¬åŒ§ã§çºçããŸãã ã¹ããŒãã¡ã³ãã«ã³ãŒãã1è¡ãããªãå Žåãæ¬åŒ§ã¯ãªãã·ã§ã³ã§ãã
if (true) console.log(" if");
åãè¡ã§ã¹ããŒãã¡ã³ããç¶ç¶ã§ãããããããã¯éåžžã«äŸ¿å©ã§ãã
var arr = ["", "", "", "", "", "", "", "", "", ""], i = arr.length - i; while (i) console.log( arr[i--] );
ããããããã¯ããã€ãã®çç±ã§åççã§ã¯ãããŸããã ãŸãããã®ææ³ã¯äžæ確ã§ãã
if (true) console.log(" if"); console.log(" if");
æå³ãããããŸããïŒ 2è¡ç®ã¯æŒç®åã®äžã«ãããŸããããéåžžã«ä¿¡ãããããã§ãã ãã©ã±ããã¯ããã«æ確ãããããããŸãã ãŸããifã«è¡ãè¿œå ããå Žåã¯ãè§ãã£ããå¿ããªãã§ãã ããã ããã«è¿œå ããæ¹ãã¯ããã«ç°¡åã§ãã ãã
ãšã©ãŒ9-èŠçŽ ã1ã€ãã€DOMã«è¿œå ãã
ã¯ããã¯ããããã¯JavaScriptã§ã¯ãããŸããã ãã ãã100ã®ãã¡99ã®å ŽåãJavaScriptã¯DOMã«åœ±é¿ããŸãã DOMã«ã¯å€ãã®ééãããããŸãããããã«æ倧ã®ééãããããŸãã
JavaScriptã䜿çšããŠDOMèŠçŽ ãåã蟌ãã®ã¯æ¥œãããŠäŸ¿å©ã§ãããæ®å¿µãªããããŒãžãããŒãããŸãã ãããã£ãŠãå€æ°ã®DOMèŠçŽ ã1ã€ãã€æ¿å ¥ããã®ã¯æªãèãã§ãã
var list = document.getElementById("list"), items = ["", "", "", ""], el; for (var i = 0; items[i]; i++) { el = document.createElement("li"); el.appendChild( document.createTextNode(items[i]) ); list.appendChild(el); // }
代ããã«è¡ãã¹ãããšã¯æ¬¡ã®ãšããã§ããããã¥ã¡ã³ãã®æçã䜿çšããŸãã ããã¥ã¡ã³ããã©ã°ã¡ã³ã-DOMèŠçŽ ãä¿åããããã®ã³ã³ããã ãã®åŸãåå¥ã«æ¿å ¥ãã代ããã«ããã¹ãŠãäžæ°ã«å®è¡ã§ããŸãã ããã¥ã¡ã³ããã©ã°ã¡ã³ãã¯ããèªäœãããŒãã§ã¯ãªãããããªããžã§ã¯ãã¢ãã«ã§è¡šç€ºããŠãäœãèµ·ãããŸããã DOMãã¬ã€ã¢ãŠãããåã«èŠçŽ ãä¿æããããã®ç®ã«èŠããªããããã¯ãŒã¯ã«ãªããŸãã 以äžã«äŸã瀺ããŸãã
var list = document.getElementById("list"), frag = document.createDocumentFragment(), items = ["", "", "", ""], el; for (var i = 0; items[i]; i++) { el = document.createElement("li"); el.appendChild( document.createTextNode(items[i]) ); frag.appendChild(el); // ! } list.appendChild(frag);
ãšã©ãŒ10-JavaScriptãåŠç¿ããŸãã
JavaScriptã¯jQueryã§ã¯ãããŸãã ã ããã£ã äžèšã®ãšã©ãŒã®ããã€ããè¡ãå ŽåãããããJavaScriptãèªãå¿ èŠããããŸãã JavaScriptã¯ã»ãšãã©ããŸãã¯ãŸã£ããåŠç¿ããã«äœ¿çšã§ããèšèªã§ãããããå€ãã®äººãJavaScriptã«æéã浪費ããŸããã ãããã®ããããã«ããªãã§ãã ããã åªããèšèªã®æç§æžãããããããã®ã§ãèšãèš³ã¯ãããŸããã ç¥ã£ãŠããã®ãjQueryïŒMooToolsãªã©ïŒã ããªããããªãã¯èªåãæªãå Žæã«çœ®ããŠããããšã«ãªããŸãã
å€ãã®äººã¯JavaScriptãé©åã«åŠç¿ããæéãç¡é§ã«ããŸããã
ãšã©ãŒ11-ãã¹ãŠã®ã«ãŒã«ã«åŸããŸã
æåŸã®æåŸã®ééã-ããªãã¯ãã¹ãŠã®ã«ãŒã«ã«åŸããŸãã ã¯ããäžéšã¯ããã«ãªã¹ããããŠããŸãã ä»ã®ãã¹ãŠã®äººãšåæ§ã«ãããããç Žãããã®ã«ãŒã«ã å®éã«ã¯ã1ã€ãŸãã¯å¥ã®ææ³ã䜿çšã§ããªãçç±ãç解ãããšã é©åãªç¶æ³ã§æ£ããé©çšã§ããããŒã«ã«ãªããŸãã åãevalãJSONã解æããå¯äžã®æ¹æ³ã§ãã ãã¡ãããã»ãã¥ãªãã£ãé©åã«ãã§ãã¯ããæ¹æ³ã¯ãããããããŸãïŒã©ã€ãã©ãªã䜿çšããæ¹ãè¯ãã§ãïŒã èªåãäœãããŠããããç¥ã£ãŠããŠããããå¿ èŠãªå Žåããæªãç·Žç¿ãã䜿ãããšãæããŠã¯ãããŸããã
ãã¡ãããééããç¯ããªãã§ãã ãã10ã
Douglas Crockford-ã¢ã¡ãªã«ã®ããã°ã©ããŒãJavaScriptã®éçºè ã人æ°ã®ããJSONãYahooã®äž»èŠãªéçºè ãããã³YahooUIã©ã€ãã©ãªã®äœæè ã