å€èªäžèœãªã³ãŒãã®é®®æãªäŸã¯JS1kã³ã³ãã¹ãã«ãããŸãããã®ç®çã¯ã1024æå以äžã§æ§æãããæé«ã®JSã¢ããªã±ãŒã·ã§ã³ãäœæããããšã§ãã JSF * ckã«ã€ããŠãåãããšãèšããŸããJSF* ckã¯ã6ã€ã®ç°ãªãæåã®ã¿ã䜿çšããŠJSã³ãŒããèšè¿°ããéåžžã«ç¬ç¹ãªããã°ã©ãã³ã°ã¹ã¿ã€ã«ã§ãã ãããã®ãµã€ãã«æçš¿ãããã³ãŒããèŠãŠãããã§äœãèµ·ãã£ãŠããã®ããç解ããããšããŠé ãæ©ãŸãã§ãããã åæ§ã®ã³ãŒããèšè¿°ãã1ãæåŸã«ãã°ãä¿®æ£ããããšãããšã©ããªããæ³åããŠã¿ãŠãã ããã
å®æçã«ããããµãŒãã£ã³ãããããã€ã³ã¿ãŒãã§ã€ã¹ãäœæãããããå Žåããã¹ãŠã®ãŠãŒã¶ãŒãå¿ èŠãšããããã§ã¯ãªã倧ããªãã©ãŒã ã«èšå ¥ããã®ã«å€ãã®æéãšå¿èãå¿ èŠã§ããããšããåãã§ãããã ã³ãŒãã«ã€ããŠãåãããšãèšããŸãã èªã¿ããããªãã°ãªãã»ã©ããããé·æé䜿çšããã®ã楜ãããªããŸãã ãŸãã¯ãå°ãªããšãã³ã³ãã¥ãŒã¿ãŒãçªã®å€ã«æŸãåºããããšãã欲æ±ã¯ãããŸããã
ãã®èšäºã§ã¯ãèœãšãç©Žãé¿ããŠã³ãŒããèªã¿ãããããããã®ããŸããŸãªããªãã¯ãšããªãã¯ãåãäžããŸãã
ããšã®èšäºïŒ https : //www.sitepoint.com/importance-of-code-that-humans-can-read/
ã³ãŒãåââé¢
ãã©ãŒã ãšã®é¡äŒŒæ§ã«æ»ããŸãïŒç»é¢ã«è¡šç€ºããããããããã«ããã©ãŒã ãéšåã«åå²ããå ŽåããããŸãã åãããšãã³ãŒãã§è¡ãããšãã§ããŸãã éšåã«åå²ãããšããžã£ã³ã°ã«ãééããããšãªãç®çã®ãã©ã°ã¡ã³ããèŠã€ãããããªããŸãã
ãã¡ã€ã«ã®æé©å
ç§ãã¡ã¯äœå¹Žãã®éãŠã§ãã®æé©åãè¡ã£ãŠããŸããããJSãã¡ã€ã«ãäŸå€ã§ã¯ãããŸããã HTTP / 2ãåŸ ã£ãŠããéã«ãããã¡ã€ãè¡ãããšã§ãè€æ°ã®ã¹ã¯ãªããã1ã€ã«çµåããŠãªã¯ãšã¹ããä¿åããŸããã ä»æ¥ã§ã¯ããã¡ã€ã«ãåŠçããã¿ã¹ã¯ãGulpãGruntãªã©ã®ããŒã«ã«ä»»ããŠã奜ããªããã«äœæ¥ã§ããŸãã ã€ãŸããå¿ èŠã«å¿ããŠããã°ã©ã ãäœæããç¹å¥ã«èšç·Žãããã¢ããªã±ãŒã·ã§ã³ã«æé©åïŒããšãã°ãé£çµã®å®è¡ïŒãå®è¡ãããããšãã§ããŸãã
// API var getUsersRequest = new XMLHttpRequest(); getUsersRequest.open('GET', '/api/users', true); getUsersRequest.addEventListener('load', function() { // - }); getUsersRequest.send(); //--------------------------------------------------- // . // , . //--------------------------------------------------- // API var getPostsRequest = new XMLHttpRequest(); getPostsRequest.open('GET', '/api/posts', true); getPostsRequest.addEventListener('load', function() { // - }); getPostsRequest.send();
æ©èœ
åå©çšå¯èœãªã³ãŒãã®ãããã¯ãäœæã§ããŸãã éåžžãé¢æ°ã®å 容ã«ãããé¢æ°ã®éå§ç¹ãšçµäºç¹ãç°¡åã«ç解ã§ããŸãã å°ããªé¢æ°ãæžãã®ã¯è¯ãç¿æ £ã§ã-10è¡ä»¥å ã é¢æ°ã®ååãæ£ããå ŽåãåŒã³åºããããšãã«äœãããããç°¡åã«ç解ã§ããŸãã 以äžã§ã¯ãåœåèŠåã«æ»ããŸãã
// API function getUsers(callback) { var getUsersRequest = new XMLHttpRequest(); getUsersRequest.open('GET', '/api/users', true); getUsersRequest.addEventListener('load', function() { callback(JSON.parse(getUsersRequest.responseText)); }); getUsersRequest.send(); } // API function getPosts(callback) { var getPostsRequest = new XMLHttpRequest(); getPostsRequest.open('GET', '/api/posts', true); getPostsRequest.addEventListener('load', function() { callback(JSON.parse(getPostsRequest.responseText)); }); getPostsRequest.send(); } // // getUsers(function(users) { // - }); getPosts(function(posts) { // - });
äžèšã®ã³ãŒãã¯èªã¿ãããã§ãã äž¡æ¹ã®æ©èœã¯ã»ãŒåãã§ããããšã«æ³šæããŠãã ããã ã ç¹°ãè¿ããªã ããšããååã«åŸãããšãã§ããŸããããã«ãããã³ãŒãå ã®é åºãç¶æã§ããŸãã
function fetchJson(url, callback) { var request = new XMLHttpRequest(); request.open('GET', url, true); request.addEventListener('load', function() { callback(JSON.parse(request.responseText)); }); request.send(); } // // fetchJson('/api/users', function(users) { // - }); fetchJson('/api/posts', function(posts) { // - });
ããããPOSTèŠæ±ã䜿çšããŠæ°ãããŠãŒã¶ãŒãäœæããå Žåã¯ã©ãã§ããããã 次ã®2ã€ã®ãªãã·ã§ã³ããããŸãã
- é¢æ°ã«ãªãã·ã§ã³ã®åŒæ°ãè¿œå ããŠãæ°ããããžãã¯ãå°å ¥ããé¢æ°ãè€éã«ããŸãã
- POSTãªã¯ãšã¹ãå°çšã®æ°ããé¢æ°ãäœæããŸããããã«ãããã³ãŒããéè€ããå¯èœæ§ããããŸãã
ãããããªããžã§ã¯ãæåããã°ã©ãã³ã°ã®ãããã§ãæ§æå¯èœãªã¯ã³ã¿ã€ã ãªããžã§ã¯ããäœæããããšã«ãããäž¡æ¹ã®ãªãã·ã§ã³ãæ倧éã«æŽ»çšã§ããŸãããã³ãŒãã®ä¿å®ã¯ããªãç°¡åã§ãã
泚 ïŒãªããžã§ã¯ãæåJavaScriptã®åºæ¬ã ããå¿ èŠãªå Žåã¯ã次ã®ãããªãã芧ãã ãã ïŒãªããžã§ã¯ãæåJavaScript ã®å®å šãªã¬ã€ã ã
ãªããžã§ã¯ãæåããã°ã©ãã³ã°
ãã°ãã°ã¯ã©ã¹ãšåŒã°ãããã³ã³ããã¹ãäŸåã®é¢æ°ã®ã¯ã©ã¹ã¿ãŒã§ãããªããžã§ã¯ããæ€èšããŠãã ããã ãã®ãããªãªããžã§ã¯ãã¯ãå¥ã®ãã¡ã€ã«ã«å®å šã«åãŸããŸãã ãã®å ŽåãXMLHttpRequestã®åºæ¬çãªã©ãããŒãäœæã§ããŸãã 2015幎ãŸã§ãJavaScriptã¯ãããã¿ã€ãæåã®èšèªã§ãããã¯ã©ã¹ããªãã£ãããšã«æ³šæããŠãã ããã ãã®äºå®ã¯ããããã¿ã€ãã®ç¶æ¿ãšãšãã«ãã³ãŒãã®ãã©ãŒãããã«é¢ããç¹å®ã®èŠåãèŠå®ããŠããŸãã
HttpRequest.js
function HttpRequest(url) { this.request = new XMLHttpRequest(); this.body = undefined; this.method = HttpRequest.METHOD_GET; this.url = url; this.responseParser = undefined; } HttpRequest.METHOD_GET = 'GET'; HttpRequest.METHOD_POST = 'POST'; HttpRequest.prototype.setMethod = function(method) { this.method = method; return this; }; HttpRequest.prototype.setBody = function(body) { if (typeof body === 'object') { body = JSON.stringify(body); } this.body = body; return this; }; HttpRequest.prototype.setResponseParser = function(responseParser) { if (typeof responseParser !== 'function') return; this.responseParser = responseParser; return this; }; HttpRequest.prototype.send = function(callback) { this.request.addEventListener('load', function() { if (this.responseParser) { callback(this.responseParser(this.request.responseText)); } else { callback(this.request.responseText); } }, false); this.request.open(this.method, this.url, true); this.request.send(this.body); return this; };
app.js
new HttpRequest('/users') .setResponseParser(JSON.parse) .send(function(users) { // - }); new HttpRequest('/posts') .setResponseParser(JSON.parse) .send(function(posts) { // - }); // new HttpRequest('/user') .setMethod(HttpRequest.METHOD_POST) .setBody({ name: 'Tim', email: 'info@example.com' }) .setResponseParser(JSON.parse) .send(function(user) { // - });
å®æ°ã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒã§ããã«å®£èšããå¿ èŠãããããšã«æ³šæããŠãã ããã äžèšã§äœæããHttpRequestã¯ã©ã¹ã¯ãå€ãã®APIåŒã³åºãã䜿çšããŠåºç¯å²ã«èšå®ã§ããããã«ãªããŸããã ãã®å®è£ ïŒã¡ãœããåŒã³åºãã®ãã§ãŒã³ïŒãããè€éã§ããããšãå€æãããšããäºå®ã«ãããããããã¯ã©ã¹èªäœã®ä¿å®ã¯ç°¡åã§ãã å®è£ ãšã³ãŒããåå©çšããèœåãšã®ãã©ã³ã¹ãèŠã€ããããã»ã¹ã¯å°é£ãªå Žåãããããããžã§ã¯ãã«ãã£ãŠç°ãªããŸãã
ãªããžã§ã¯ãæåããã°ã©ãã³ã°ãžã®å€§ããªè¿œå ã¯ããã¶ã€ã³ãã¿ãŒã³ã§ãã ããèªäœã§ã¯ãèªã¿ãããã¯åäžããŸããããäžè²«æ§ã¯ãããå®çŸããŸãïŒ
人ã«åªããæ§æ
ãã¡ã€ã«ãé¢æ°ããªããžã§ã¯ã-ãããã¯åãªã倧ãŸããªã¬ã€ãã©ã€ã³ã§ãã ã³ãŒããç°¡åã«ã¹ãã£ã³ã§ããŸãã ããããã³ãŒããèªã¿ãããããããšã¯ãã¯ããã«åŸ®åŠãªæè¡ã§ãã 现éšã现ããããããšã§ãç»åãå®å šã«å€æŽã§ããŸãã ããšãã°ãå€ãã®å ŽåãåçŽç·ã䜿çšãããšãã£ã¿ãŒã§ã¯ãè¡ã®é·ãã¯80æåã«å¶éãããŠããŸãã ããããä»ã«ãå€ãã®ãã¥ã¢ã³ã¹ããããŸãïŒ
åœå
é©åãªåœåã®ãããã§ãå€æ°ã®çš®é¡ããã®é¢æ°ã®æ©èœãç解ããããšãªããç¹å®ã®èŠçŽ ãå³åº§ã«èªèããããšãã§ããŸãã
é¢æ°ã¯éåžžãã©ã¯ãã®ã¹ã¿ã€ã«ã§åŒã³åºãããŸããæåã«åè©ã次ã«äž»èªãæ¥ãŸãã
function getApiUrl() { /* ... */ } function setRequestMethod() { /* ... */ } function findItemsById(n) { /* ... */ } function hideSearchForm() { /* ... */ }
å€æ°åã«ã€ããŠã¯ã éãã©ãããææ³ã䜿çšããŠã¿ãŠãã ãããæåã«ä»¶åã衚瀺ããã次ã«ããããã£ã衚瀺ãããŸãã
var element = document.getElementById('body'), elementChildren = element.children, elementChildrenCount = elementChildren.length; // , âcolorâ var colorBackground = 0xFAFAFA, colorPrimary = 0x663399; // , âbackgroundâ var backgroundColor = 0xFAFAFA, backgroundImages = ['foo.png', 'bar.png']; // var headerBackgroundColor = 0xFAFAFA, headerTextColor = 0x663399;
ãã®ãããªå Žåãé£æ³é åïŒJSã®ãªããžã§ã¯ãïŒã䜿çšããæ¹ãé©åã§ãããã®åŸãè²ã®æ¥é èŸãåãé€ãããšãã§ããèªã¿ããããåäžããŸãã
var colors = { 'backgroundRed' : red, 'primary': greeen };
ãŸããéåžžã®å€æ°ãšç¹æ®å€æ°ã®åºå¥ã匷調ããããšãéèŠã§ãã ããšãã°ãå®æ°åã¯å€ãã®å Žåã¢ã³ããŒã¹ã³ã¢ã§å€§æåã«ãªããŸãã
var URI_ROOT = window.location.href; , . function FooObject { // ... }
ç¥èªã§ã¯ããã¥ã¢ã³ã¹ããããŸãã 誰ãã倧æåã§ã®ã¿ããã£ã¡ã«ã¹ã¿ã€ã«ã§èª°ããæžããŠããŸãã åè ã®å Žåãç¥èªãèªèããã®ãé£ããå ŽåããããŸãã
ããããäžè¬ã«ããã¹ãŠã¯éçºè ãŸãã¯ããŒã ãæ¡çšããã³ãŒãã£ã³ã°ã¹ã¿ã€ã«ã«äŸåããŸãã åžžã«éžæããã1ã€ã®ã¹ã¿ã€ã«ãé å®ãããã®ã³ã³ãã©ã€ã¢ã³ã¹ã確èªãããªã³ã¿ãŒã䜿çšããå¿ èŠããããŸãã
ã³ã³ãã¯ãã§æé©å
å€ãã®ã³ãŒãããŒã¹ã§ã¯ãæåæ°ãæžãããããã¢ã«ãŽãªãºã ã®ããã©ãŒãã³ã¹ãåäžãããããã«èšèšããããç¹å¥ãªãã³ãŒããèŠã€ããããšãã§ããŸãã
ã³ã³ãã¯ããªã³ãŒãã®äŸã¯ãåäžè¡ã®ã¹ã¯ãªããã§ãã æ®å¿µãªããã圌ãã¯ãã°ãã°ããã¯ããããŸããªæ§æã䜿çšããŸãã ååãšããŠããã¹ããããäžé æŒç®åã䜿çšãããŸãã ã³ã³ãã¯ãã§ããã«ãããããããéåžžã®
if
ãšã¯ç°ãªããäœãããŠããã®ããç解ããããšããŠæ°ç§ãè²»ããå¿ èŠããããŸãã ãã®ãããæ§æã®ç¥èªã«ã¯æ³šæããŠãã ããã
é©åãªã¢ãããŒãã¯ãæ¬çªç°å¢ã§ã³ãŒããçž®å°ããããšã§ãã ãã¡ã€ã«ã®ééã¯è»œãããã©ãŠã¶ã«ããé«éã«ããŒããããããŒãžå šäœã®ããŒãæéãççž®ãããŸãã ããããçç£ã«äŸ¿å©ãªçž®å°ãã¡ã€ã«ã¯ãéçºã«ãããŠå®å šã«äžäŸ¿ã§ãã ãããã£ãŠãprodãšvirgoã§äœ¿çšãããJSãåé¢ããåãã¡ã€ã«ã®2ã€ã®ããŒãžã§ã³ïŒçž®å°ããã³å®å šïŒãä¿åããæ§æã§æå®ãããç°å¢ã«åŸã£ãŠäœ¿çšãåãæ¿ããããšãã§ããŸãã
// , - ! var state = isHidden ? 'hidden' : isAnimating ? 'animating' : ''; // , - ! var state = ''; if (isAnimating) state = 'animating'; if (isHidden) state = 'hidden';
ããã©ãŒãã³ã¹ãæ¹åããããã«èšèšããããã€ã¯ãæé©åã¯ãå€ãã®å Žåå¹æçã§ã¯ãããŸããã ãããŠãã»ãšãã©ã®å Žåãçç£æ§ã®äœããã®ãããèªã¿ã«ãããªããŸãã
// $el[0].checked; // , // : http://jsperf.com/prop-vs-ischecked/5 $el.prop('checked'); $el.is(':checked'); $el.attr('checked');
è€æ°ã®ãã€ã³ããã§ãã¯ã®å Žåãã¹ã€ããã䜿çšããããšããå§ãããŸãããã€ã³ãå€ã®ãã§ãã¯ãçºçããããšã¯æå³çã«æ確ã§ãã
JavaScriptã³ã³ãã€ã©ãŒã¯ã³ãŒãã®æé©åãããŸãè¡ã£ãŠãããé·å¹Žã«ããã£ãŠãã®æé©åãè¡ã£ãŠããŸããã æé©åãããã³ãŒããšæé©åãããŠããªãã³ãŒãã®éããç®ç«ããªãå Žåãæ°åãŸãã¯æ°çŸäžã®æäœã䜿çšãããšãã«ãã®éšåã芳å¯ãããå Žåãèªã¿ããããåªå ããæ¹ãè¯ãã§ãã
éã³ãŒã
ãããç®èã ãšèããŠãã ãããããããã³ãŒããèªã¿ãããããæåã®æ¹æ³ã¯ãå®è¡äžå¯èœãªæ§æãè¿œå ããããšã§ãã ãããéã³ãŒããšåŒã³ãŸãããã ãã ããJSã¯äžè¬çã«ãç¡èŠãã¹ãã§ã¯ãªãã³ãŒããã©ãŒãããæšæºãåãå ¥ããŠããããšãå¿ããªãã§ãã ããã 以äžã§èª¬æããŸãã
ããŒã¿
確ãã«ãå°ãªããšãäžåºŠã¯ãã¹ãŠã®éçºè ãèªåã®äººçã§ã³ãŒããå¥ã®éçºè ã«æžã蟌ãã®ãæäŒã£ããããã¹ãŠã®ã¹ããŒã¹ãšã€ã³ãã³ããåé€ãããçž®å°ã³ãŒããæ€æ»ãããããŸããã åããŠãããã¯ããªãã«ãšã£ãŠé©ãã ã£ãã«éããããŸããã ãã¶ã€ã³ãã¿ã€ãã°ã©ãã£ãªã©ã®ããŸããŸãªèŠèŠåéã§ã¯ã空ãé åã¯å¡ãã€ã¶ããšåããããéèŠã§ãã ã»ãšãã©ã®å Žåããããã®éã®åŸ®åŠãªãã©ã³ã¹ãèŠã€ããããšæãã§ãããã ãã®ãã©ã³ã¹ã«é¢ããæèŠã®ç¯å²ã¯ãäŒç€ŸãããŒã ãããã³ç¹å®ã®éçºè ã«ãã£ãŠç°ãªããŸãã 幞ããªããšã«ãäžè¬çã«åãå ¥ããããŠããã«ãŒã«ãããã€ããããŸãã
- 1è¡ã«1ã€ã®åŒã䜿çšããŸãã
- ã€ã³ãã³ãããŠãããã¯ã匷調衚瀺ããŸãã
- è¿œå ã®ã³ãŒããã¬ãŒã¯ã䜿çšããŠãã³ãŒããåé¢ã§ããŸãã
ä»ã®ã«ãŒã«ã¯ãäžç·ã«åã人ãšè©±ãåãå¿ èŠããããŸãã ãã ããèšèšããã³ãŒãã®ã¹ã¿ã€ã«ã«é¢ä¿ãªããéèŠãªã®ã¯äžè²«æ§ã§ãã
function sendPostRequest(url, data, cb) { // var requestMethod = 'POST', requestHeaders = { 'Content-Type': 'text/plain' }; // , XMLHttpRequest var request = new XMLHttpRequest(); request.addEventListener('load', cb, false); request.open(requestMethod, url, false); request.send(data); }
ã³ã¡ã³ã
ã€ã³ãã³ããšåæ§ã«ãã³ã¡ã³ãã¯ã³ãŒããå®å šã«ãŸã°ãã«ããããšãã§ããŸãã åæã«ãã³ãŒãã®èª¬æãå«ãŸããŠããŸãã å¿ ãã³ã¡ã³ããè¿œå ããŠãã ããïŒ
- éèªæãªã³ãŒãã®èª¬æãšåŒæ°ã
- ãã®ä¿®æ£ã䜿çšããŠä¿®æ£ããããã°ã¯ããœãŒã¹ã瀺ããŠããŸãã
// var sum = values.reduce(function(previousValue, currentValue) { return previousValue + currentValue; });
ãã¹ãŠã®ä¿®æ£ãæãããªããã§ã¯ãããŸããã è¿œå æ å ±ã®å°å ¥ã«ãããå€ãã®ããšãæããã«ãªããŸãã
if ('addEventListener' in element) { element.addEventListener('click', myFunc); } // IE8 .addEventListener, // .attachEvent // http://caniuse.com/#search=addEventListener // https://msdn.microsoft.com/en-us/library/ms536343%28VS.85%29.aspx else { element.attachEvent('click', myFunc); }
é¢æ°ã®ããã¥ã¡ã³ãã䜿çšãããšãã¯ãã³ãŒãå ã§ã³ã¡ã³ãã䜿çšãããã³ã¡ã³ããå¿ èŠãšããªãè€æ°è¡ã®é¢æ°ãäœæããããšããå§ãããŸãã äœæ¥äžã¯ã todoã³ã¡ã³ãã䜿çšã§ããŸããå€ãã®IDEã¯ãããã®æ€çŽ¢ããµããŒãããŠããŸãã todoã䜿çšãããšããã¡ã€ãã©ã€ãºãŸãã¯å®äºããå¿ èŠã®ããã³ãŒãããŒã¹ãããŒã¯ãããŸãã ãã®ãããä»ã®éçºè ã¯ãèšç»ãšèšèšã§å®ããããããžãã¯ã§ããã¢ãããŒããç解ãããããªããŸãã todoãé©åãªå Žæã«çœ®ãããšã¯éåžžã«äŸ¿å©ã§ãã
çµ±åããã¥ã¡ã³ã
ãªããžã§ã¯ãæåãœãããŠã§ã¢ãäœæãããšããåã蟌ã¿ããã¥ã¡ã³ãã¯ã³ãŒããããèªç±ã«ããéåžžã®ã³ã¡ã³ãã«ããããšãã§ããŸãã ãŸããããããã£ãšã¡ãœããã®æ©èœã®ç®çãšè©³çŽ°ã«ã€ããŠã説æããŸãã å€ãã®IDEã¯ãããã¥ã¡ã³ãçæããŒã«ã䜿çšãããã³ããšããŠçµã¿èŸŒã¿ã®ããã¥ã¡ã³ãã䜿çšããŸãïŒ çç±ã«é¢ä¿ãªããã³ãŒãã§ããã¯ãèšè¿°ããããšã¯åªãããœãªã¥ãŒã·ã§ã³ã§ãã
/** * HTTP- * @constructor * @param {string} url */ function HttpRequest(url) { // ... } /** * * @param {Object} headers * @return {HttpRequest} */ HttpRequest.prototype.setHeaders = function(headers) { for (var header in headers) { this.headers[header] = headers[header]; } // return this; };
解説ã«ã¯param {Object}ããããŒã瀺ãããŠããŸããããã®ãã©ã¡ãŒã¿ãŒã«ä¿åãããŠæž¡ããããã®ã®èª¬æãæäŸããæ¹ãé©åã§ãã ãã®æ å ±ã¯ããã©ã¡ãŒã¿ãŒèªäœã®çŽåŸã«ç€ºãããšããå§ãããŸãã ãããHolds success request headersïŒ200ïŒã§ãããšããŸãããã 次ã«ãã³ã¡ã³ãã¯æ¬¡ã®ããã«ãªããŸãã
@param {Object} headers Holds success request headers( 200 )
æ©èœã«é¢ããããã¥ã¡ã³ãã®åœ¢åŒã¯ããŸããŸã§ãããããŒã å šäœãåã圢åŒã䜿çšããããšãéåžžã«éèŠã§ãã éçºè ã1人ã®å Žåãç°ãªãå Žæã§åã圢åŒã䜿çšããå¿ èŠããããŸãã ä»®ãã©ã¡ãŒã¿ã®èª¬æã§ã¯ãä»®ãã©ã¡ãŒã¿ã®TIã瀺ãããšãå¿ é ã§ãã ããã€ãã®IDEã¯ãééã£ãåã®å€æ°ãæž¡ãããå Žåããã®å Žã§ããã³ããã衚瀺ããŸãã JSã¯åçãªåæå®ãåããèšèªã§ãããIDEããšã©ãŒãã¹ããŒãããšããäºå®ã§ã¯ãããŸãããããã®ãããªããã¥ã¡ã³ãã解æãããšãã«å€ãã®ãšã©ãŒãåé¿ã§ããŸãã
ã³ãŒã«ããã¯ã®ãªããªã
ã€ãã³ããšéåæåŒã³åºãã¯JavaScriptã®åªããæ©èœã§ãããå€ãã®å Žåãã³ãŒãã®å¯èªæ§ãæãªããŸãã
éåžžãéåæåŒã³åºãã«ã¯ã³ãŒã«ããã¯ã䌎ããŸãã æã«ã¯ããããé çªã«å®è¡ããå¿ èŠããããæã«ã¯ããããã¹ãŠã®æºåãæŽããŸã§åŸ ã€ã»ããè¯ãå ŽåããããŸãã é¢æ°ã®ããã¥ã¡ã³ãã§ãã³ãŒã«ããã¯é¢æ°ãžã®åç §ãäœæããå¿ èŠããããŸããããã«ãããã³ãŒã«ããã¯é¢æ°ã«ç°¡åã«ç§»åãããã説æã確èªãããã§ããŸãã
function doRequest(url, success, error) { /* ... */ } doRequest('https://example.com/api/users', function(users) { doRequest('https://example.com/api/posts', function(posts) { // - }, function(error) { // /api/posts }); }, function(error) { // /api/users });
äž¡æ¹ã®åé¡ã解決ããããã«ãPromiseãªããžã§ã¯ããES2015ïŒES6ãšãåŒã°ããŸãïŒã«å®è£ ãããŸããã
function doRequest(url) { return new Promise(function(resolve, reject) { // // resolve(response) // reject(error) }); } // doRequest('https://example.com/api/users') // , .then() .then(function(users) { /* ... */ }) // Promise reject(), .catch() .catch(function(error) { /* ... */ }); // Promise Promise.all([ doRequest('https://example.com/api/users'), doRequest('https://example.com/api/posts') ]) .then(function(responses) { /* ... */ }) .catch(function(error) { /* ... */ });
ã³ãŒãã®äžéšãè¿œå ããå¿ èŠããããŸããããéåžžã¯æ£ãã解éããæ¹ãç°¡åã§ããã Promiseã®è©³çŽ°ã«ã€ããŠã¯ã JavaScript Goes AsynchronousïŒand It's AwesomeïŒãã芧ãã ããã
ES6 / ES2015
ES2015ä»æ§ãèªãã å Žåããã®èšäºã®ãã¹ãŠã®ã³ãŒãäŸãå€ãããŒãžã§ã³ã®ãã®ã§ããããšã«ãæ°ã¥ããããããŸããïŒPromiseãªããžã§ã¯ããé€ãïŒã ES6ã«ã¯éåžžã«å¹ åºãå¯èœæ§ããããŸãããã²ã©ãèªã¿ãããã«é¢ããŠã¯å€ãã®äžæºããããŸãã
倪ãç¢å°ã®æ§æã¯ã芪ã¹ã³ãŒããã
this
ãç¶æ¿ããé¢æ°ãå®çŸ©ããŸãã å°ãªããšãããã®ããã«äœæãããŸããã ãŸããéåžžã®é¢æ°ãå®çŸ©ããããã«äœ¿çšããã®ãé åçã§ãã
var add = (a, b) => a + b; console.log(add(1, 2)); // 3
å¥ã®äŸã¯ãæ®ããšã¹ãã¬ããã®æ§æã§ãã
/** * * @param {Array} numbers * @return {Number} */ function add(...numbers) { return n.reduce(function(previousValue, currentValue) { return previousValue + currentValue; }, 0); } add(...[1, 2, 3]); /** * a, b c * @param {Number} a * @param {Number} b * @param {Number} c * @return {Number} */ function add(a, b, c) { return a + b + c; } add(1, 2, 3);
ES2015ä»æ§ã§ã¯ãéåžžã«äŸ¿å©ã§ããããããŸãã§ãæã«ã¯æ··ä¹±ãæãæ§æãæ¹è©å®¶ã®ã¿ãŒã²ããã«ãªã£ãŠããŸãã ããããç§ãã¡ã¯ãããã®æ©äŒãæŸæ£ããããšã«ã€ããŠè©±ããŠããã®ã§ã¯ãªããåã«æ³šæããŠãããã䜿çšããŠããŸãã
ãããã«
ã³ãŒãã®å¯èªæ§ãšä¿å®æ§ãç¶æããã«ã¯ããããžã§ã¯ãã®ããããåŽé¢ãèŠããŠããå¿ èŠããããŸãã ãã¡ã€ã«ã·ã¹ãã ããå°ããªæ§æããªãšãŒã·ã§ã³ãŸã§ããã¹ãŠãéèŠã§ãã ããã¯ãååå è ã«ãã¹ãŠã®ã«ãŒã«ãåžžã«é å®ãããããšãé£ãããããããŒã ã§äœæ¥ããå Žåã«ç¹ã«é¡èã§ãã åé¡ã®äžéšã¯ã³ãŒãã®ä¿®æ£ã«ãã£ãŠè§£æ±ºãããŸãããããã§ããšã©ãŒã®å¯èœæ§ããããŸãã 幞ããªããšã«ãããã€ãã®äŸ¿å©ãªããŒã«ããããŸãã
- JSHintã¯ãã³ãŒãã®ãšã©ãŒãåé¿ããJavaScriptãªã³ã¿ãŒã§ãã
- ã€ãã£ãªããã£ãã¯ã¯ã³ãŒããã¹ã¿ã€ãªã³ã°ããããã®äžè¬çãªæšæºã§ãããéèªçã«åŸãå¿ èŠã¯ãããŸããã
- EditorConfig-ã¯ãã¹ãšãã£ã¿ãŒã³ãŒãã¹ã¿ã€ã«ã®å®çŸ©ã
ã³ãŒãã®è€éããåæããããšã¯éåžžã«éèŠã§ãããåæã«ã¯ããŸããŸãªã¢ãããŒãããããŸãã 詳现ã«ã€ããŠã¯ã http ïŒ //jscomplexity.org/complexityãã芧ãã ããã
å質ãšã¹ã¿ã€ã«ãç¶æããããã®ããŒã«ã«å ããŠãèªã¿ããããæ¹åããããã®ããŒã«ãããã€ããããŸãã ããŸããŸãªæ§æ匷調ã¹ããŒã ãè©Šããããããããã䜿çšããŠã¹ã¯ãªãããéå±€çã«è©äŸ¡ããŸãïŒ Atom ã Brackets ïŒã ããšãã°ã httpsïŒ//github.com/airbnb/javascriptã¯ãããªãäžè¬çãªJSã³ãŒãã®ãã©ãŒãããæšæºã§ãã ã³ãŒãã®ãã©ãŒããããšæšæºã¯äœ¿çšããããŒã«ã«äŸåããããšã«æ³šæããŠãã ããã ããšãã°ãReactãšJSXã®å Žåãä»ã®ã¹ã¿ã€ã«èŠä»¶ããããŸãã