æ°æ¥åã TheShockãšç§ã¯ãJavaScriptïŒã¢ãŒããã¯ãã£ããã¬ãŒã ã¯ãŒã¯ãåé¡ïŒã«é¢ãã質åãåéããããã㯠ãäœæããŸããã ãããã«çããæã§ãã ã³ã¡ã³ããšã¡ãŒã«ã®äž¡æ¹ã§å€ãã®è³ªåãåããŸããã çãã®ãã®æåã®éšåã¯ãç§ãåŸã質åã§ãã
1.ãããã¿ã€ãã®ç¶æ¿ã ã©ã®ããã«æ©èœããŸããïŒ
ãããã¿ã€ãã¢ãã«ã®ç解ã«åé¡ãããããå€å žçãªãã¯ã©ã¹ã«æ £ããŠããŸãããJSãå匷ããããšã«ããŸããã å¯èœã§ããã°ããã¿ãŒã³ã®åœ¢åŒã§ããã¯ã©ã¹ããæ§ç¯ããããã®å¯èœãªãªãã·ã§ã³ãã¡ãœãããšå€æ°ã®ç°ãªãã¬ãã«ã®å¯èŠæ§ã説æããèšäºãæžããŠãã ããã ãã®ãããªèšäºã¯èšå€§ãªæ°ã§èŠã€ããããšãç解ããŠããŸãããJSã§ã¯å¯èŠæ§ã®ã¬ãã«ã¯ãå¿ èŠãªããããšãç解ããŠããŸãã
çãã¯éåžžã«é·ãã£ãã®ã§ãå¥ã®ãããã¯ãäœæããŸããïŒ JavaScriptã®åºç€ãšèª€è§£
2.ãªããžã§ã¯ããäœæããããã®æã䟿å©ãªã¢ãã«ã¯äœã§ããïŒ
æ°èŠã®å Žåããšã©ãŒããã©ã®ããã«ä¿è·ãããŸããïŒ
1.ã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ç§ã¯åžžã«å€§æåã§æžããŸãã
2. FunctionNameã®ãã®instanceofã䜿çšããŠäœæã®åŠ¥åœæ§ã確èªããŸãïŒããã©ãŒãã³ã¹äžã®çç±ããããã®instanceof arguments.calleeãé¿ããŸãïŒã
3. 2çªç®ãšäŒŒãŠããŸããããŠã£ã³ããŠã§ç¢ºèªããŸãã ååãããŒãã³ãŒãã£ã³ã°ãããããã©ãŠã¶å€ç°å¢çšã®ã¹ã¯ãªãããèšè¿°ãããããŸããã
ããè¯ãããã芪ãã¿ããããã€ããªãã®ãŒçã«æ°ãããã®ãéããŠãªããžã§ã¯ããäœæããŸãã ãã¶ã€ããŒã¯å€§æåã«ããå¿ èŠããããŸãã
ç§ã¯èŠåã«é Œãããšã奜ã¿ãã³ã³ã¹ãã©ã¯ã¿ãŒå ã§ããããã§ãã¯ããŸãã-ç§ã¯ã³ã³ã¹ãã©ã¯ã¿ãŒãæ°ãããã®ãªãã§åŒã³åºãããããã°ããŒãã«ã«é£ãã§ããŸã-ããã¯ãæãè èªèº«ããæå³ããŸã ãããŠã決ããŠç§ã¯newã§ãšã©ãŒã奚å±ããŸãã-äžéšã®äººã ã¯ãããã°ããŒãã«ãªãã®ã§ãããã©ããã確èªãã次ã«ãŠãŒã¶ãŒãnewãªãã§ã³ã³ã¹ãã©ã¯ã¿ãåŒã³åºããã³ã³ã¹ãã©ã¯ã¿å ã«ãªããžã§ã¯ããäœæããŠãããè¿ããŸã-ããã¯ãšã©ãŒãšã€ããªãã®ãŒçã«ééã£ãã¢ãããŒãã§ãã
var Obj = function () { "use strict"; this.pew = 100; }; // new Obj.pew++; // Obj(); // TypeError: this is undefined
newã¯ãã¡ã¯ããªã¡ãœãããããã³çãã³ã³ã¹ãã©ã¯ã¿ã«ã¯åãå ¥ããããŸãã-jQuery
ã³ãŒããèŠçŽããŸãã
// : , , use strict var Obj = function () { "use strict"; this.pew = 100; }; // : var Obj = function () { if (!(this instanceof Obj)) { return new Obj(); } this.pew = 100; };
3. JSã§ã©ã®ããŠã¹ãã¿ã³ãæŒãããããå€æããæ¹æ³
mousedown mouseup
ã€ãã³ã
mousedown mouseup
ãã¹ãŠã®ããŠã¹ãã¿ã³ã«ãã£ãŠããªã¬ãŒãããŸãã
click
ã¯å·Šã®ã¿ã§ãã ã€ãã³ããã³ãã©ãŒã§ã
event.button
ãã¿ã³ã®ã³ãŒãããã§ãã¯ããŠãæŒããããã¿ã³ãèŠã€ããå¿ èŠããããŸãã
ïŒ0-å·Šã1-äžå€®ã2-å³ïŒã IE8ã§ã¯ããã¹ãŠãééã£ãŠããŸããã³ãŒããåç §ããŠãã ããã
var button = document.getElementById('button'), // 0 1 2 buttonMap = ['Left', 'Middle', 'Right'], handler = function (event) { event = event || window.event; alert(buttonMap[event.button] + ' id: ' + event.button); }; if (button.addEventListener) { button.addEventListener('mousedown', handler, false); } else { // IE 0 1 2 3 4 buttonMap = ['???', 'Left', 'Right', '???', 'Middle']; button.attachEvent('onmousedown', handler); }
jQueryã¯ãã®IEã®æ¬ é¥ãä¿®æ£ãã
event.button
ã®éæ³ã®ä»£ããã«
event.which
ããã§ãã¯ãã䟡å€ããã
event.button
$('button').mousedown(function (event) { alert(['Left', 'Middle', 'Right'][event.which]); });
äŸïŒ jsfiddle.net/azproduction/W2XgH
è©³çŽ°ïŒ www.quirksmode.org/js/events_properties.html段èœãã©ã®ããŠã¹ãã¿ã³ãã¯ãªãã¯ãããŸãããïŒã
jQuery event.whichïŒ api.jquery.com/event.which
4.ããŒããŒãã®ããŒã¹ãããŒã¯ã€ãã³ããã€ã³ã¿ãŒã»ããããããšã¯ã§ããŸããïŒ
ãã©ãŠã¶ããã®åŸãŠã£ã³ããŠãã¹ã¯ããŒã«ããªãããã«ãJavaScriptã§ããŒããŒãã®ããŒã¹ãããŒã¯ïŒäžç¢å°ãäžç¢å°ïŒã®ã€ãã³ãããã£ããã£ããããšã¯å¯èœã§ããïŒ å¯èœãªå Žåããã®åäœã®ãã©ãŠã¶ãŒéã«æ©èœã¯ãããŸããïŒ ããšãã°ãç»é¢å šäœã«åãŸããªãããŒãã«ããããŸãããç·ã«æ²¿ã£ãŠç§»åããã«ã¯ããŒããŒãã®ç¢å°ã䜿çšããŸãã ãã©ãŠã¶ãããŒãžãã¹ã¯ããŒã«ããªãããšãå¿ èŠã§ãã
ãããè¡ãã«ã¯ãããããããã©ã«ãã®ã¢ã¯ã·ã§ã³ããã£ã³ã»ã«ããå¿ èŠããããŸãïŒäžç¢å°ãšããŠã¹ãã€ãŒã«ã§ãŠã£ã³ããŠãã¹ã¯ããŒã«ããå³ããŠã¹ãã¿ã³ã§ã³ã³ããã¹ãã¡ãã¥ãŒã衚瀺ãã
form.submit()
ãã¯ãªãã¯ããŠ
form.submit()
ãå®è¡ããŸããå ¥åãã¯ãªãã¯ãããšããã©ãŒã«ã¹ãååŸãããŸãããã©ãŠã¶ã¯ãªã³ã¯ãªã©ãä»ããŠã¢ã³ã«ãŒã«ç§»åããŸãã
jQueryã䜿çšãããšã次ã®ããã«å®è¡ã§ããŸãã
// keypress, keyup $(window).bind($.browser.opera ? 'keypress' : 'keyup', function (event) { event.preventDefault(); // return false; });
éèŠãªãã€ã³ãããããŸãã defaultActionãå®è¡ããåã«
preventDefault()
å®è¡ããå¿ èŠããããŸãã ããšãã°ãå ¥åãã¯ãªãã¯ãããšãã«ãã©ãŒã«ã¹ãäžããããªãå Žåã¯ãdefaultAction-mousedownãå®è¡ããããŸã§ããã§ãŒã³å ã®ã€ãã³ãã«ãã³ãã©ãŒããã³ã°ãããå¿ èŠããããŸãã
$('input').bind('mousedown', function (event) { event.preventDefault(); // return false; });
ã€ãã³ãã®ãã§ãŒã³èªäœã¯æ¬¡ã®ãšããã§ãã
1.ããŠã¹ããŠã³
2.ãã©ãŒã«ã¹ïŒãã©ãŒã«ã¹ã®åã«ãå¥ã®ãªããžã§ã¯ãã§ãŒãããæ©èœããŸãïŒ
3. mouseup
4.ã¯ãªãã¯
ã€ãã³ãããã©ãŒã«ã¹ä»¥äžã«çµããšãäœãèµ·ãããŸããã defaultActionã¯ãããŠã¹ããŠã³åŸã«æ©èœããŸãã
5.ãã®ããŒããã€ã³ããããŠããå ŽåãESCãæŒãããšãã«gifã¢ãã¡ãŒã·ã§ã³ãåæ¢ããåé¡ã解決ããã«ã¯ã©ãããã°ããã§ããïŒ
äžèšã®åçãåç §ããŠãã ããã äžéšã®ãã©ãŠã¶ã§ã¯ãEscããŒãæŒããšãgifã¢ãã¡ãŒã·ã§ã³ãåæ¢ããããŒãžã®èªã¿èŸŒã¿ãåæ¢ããŸã-ããã¯ããã©ã«ãã®ã¢ã¯ã·ã§ã³ã§ãã
ããã©ã«ãã¢ã¯ã·ã§ã³
event.preventDefault()
å ã«æ»ãå¿ èŠããããŸãã
$(document).bind($.browser.webkit ? 'keydown' : 'keypress', function (event) { // Esc if ((event.which || event.keyCode) === 27) { event.preventDefault(); // return false; } });
6.ãããŠãã¯ããŒãžã£ãŒãäœæãããæŒç®åïŒïŒã¯äœã§ããïŒ
æ¬åŒ§ã䜿çšãããšãããŒãµãŒã¯é¢æ°ã®åŸã®æ¬åŒ§ã®å 容ïŒã°ã«ãŒãåæŒç®åãŸãã¯é¢æ°åŒã³åºãæŒç®åïŒãç解ã§ããŸãã
ãã®ããã«ãããšïŒ
function () { // source }()
ãã®å Žåãé¢æ°åããªãããã«SyntaxErrorãçºçããŸãïŒå®£èšé¢æ°ã«ã¯åžžã«ååãå¿ èŠã§ãïŒã
ååãè¿œå ããå ŽåïŒ
function foo() { // source }()
2çªç®ã®å Žåãååã¯ïŒfooïŒã«èšå®ãããçè«çã«ã¯ãé¢æ°å®£èšã¯ããŸãããã¯ãã§ãã ãã ãããŸã åŒãšã©ãŒã®ãªãã°ã«ãŒãåæŒç®åã«é¢ããŠã¯ãæ§æãšã©ãŒããŸã ãããŸãã ãã®å Žåãé¢æ°åŒã³åºããã©ã±ããã§ã¯ãªããé¢æ°å®£èšã«ç¶ãã®ã¯ã°ã«ãŒãåæŒç®åã§ãïŒ
ParseErrorãé²ãæ¹æ³ã¯ä»ã«ããããŸã-é¢æ°ãåŒã®ç¶æ ã«ãããã€ãŸã ããããŸãã«é¢æ°åŒã§ããããšãããŒãµãŒã«ç€ºããŸãã
TheShockããïŒ
!function () { // source }(); +function () { // source }(); [function() { // source }()]; var a = function () { // source }();
å«ãŸããŠããŸãã jQueryã§ã ããŒã«ã«ã¹ã³ãŒããæã€1ã€ã®ãããã¯å ã®ãã¹ãŠã®ã³ãŒããéžæã§ããŸãã ããã«ãããå éšå€æ°ãžã®ã¢ã¯ã»ã¹ãé«éåãããã°ããŒãã«ããŒã ã¹ããŒã¹ãæ··ä¹±ãããããšãã§ããŸããæãéèŠãªããšã¯ããããã¡ã€ã€ã«ããå§çž®ã®æ¹ãåªããŠããããšã§ãã habrahabr.ru/blogs/jquery/118564
æædsCode Subtleties ECMA-262-3ã«åºã¥ããŸãã ããŒã5.æ©èœã -ãæ¬åŒ§ã«ã€ããŠããšãã質å
ç¶ããèªã ïŒ kangax.github.com/nfe
7. XHRã§ã®è»¢éã³ãŒã
ãµãŒããŒã¯ãajaxã·ã¹ãã ã§ã®ãŠãŒã¶ãŒã®ã¢ã¯ã·ã§ã³ã«ãalertïŒ 'Boom !!!'ïŒ;ããšããå¿çãéä¿¡ããŸãã ã¯ã©ã€ã¢ã³ãã§ã¯ãåä¿¡ããå¿çãevalïŒïŒãä»ããŠå®è¡ãããå®è¡ãããŸãã ãã®ããŒã¿è»¢éã®ååã¯äœã§ããïŒ ããã¯JSONã§ã¯ãªããXMLã§ã¯ãªããHTMLã§ã¯ãããŸããã
ããã«ã¯ååã¯ãããŸããã å®éãããã¯éåžžã«æªãã¢ãããŒãã§ããPHPã³ãŒããããŒã¿ããŒã¹ã«ä¿åããæ¯åè©äŸ¡ããã®ãšåããããæªãã§ãã æ¡ä»¶ä»ãã®éæ©å¯æ§ã«å ããŠããã®ãããªã¢ãŒããã¯ãã£ã«ã¯åŒ·åãªäžè²«æ§ããããå°æ¥ã¯ããçŽããå°é£ã«ãªããŸãã ãã®ãããªã¢ãã«ã§ã¯ãããŒã¿+ã³ãŒã+è¡šçŸãªã©ã®æ··ä¹±ãçºçããŸããäœããããçŽãã«ã¯ããã®ãã€ãã解ããå€æ°ã®æ¥ç¶ãèæ ®ããŠå€æŽãå ããæ··ä¹±ãããå¿ èŠããããŸãã ç§ã¯ãã®ãããªæ··ä¹±ããæ©èœã®äžéšãåŒãè£ãããšã«ã€ããŠè©±ããŠããŸãã...
ã³ãŒããµããŒããç°¡çŽ åããã«ã¯ãã·ã¹ãã ã®åéšåãæ倧éã«åé¢ããæ¥ç¶æ°ïŒäŸåé¢ä¿ïŒãæžããå¿ èŠããããŸãã 匱ãæ¥ç¶ãååŸããããã«ïŒã¢ããªã±ãŒã·ã§ã³ã®äžéšãå¯èœãªéãç°¡åã«åãé¢ããã亀æãããã§ããå ŽåïŒãã€ãã³ãããããšãã°MVCã¢ããªã±ãŒã·ã§ã³ã¢ãŒããã¯ãã£ãå°å ¥ãããŸãã
èªãïŒ
ãããŠåã³MVCã«ã€ããŠ
Webã¢ããªã±ãŒã·ã§ã³ã§ã®ã€ãã³ãé§ååã¢ãã«ã®é©çš
Backbone.jsã䜿çšããè€éãªã€ã³ã¿ãŒãã§ã€ã¹ã®äœæ
8.ã¹ã¯ãªããå šäœããã³ã°ãããããšãªããã³ãã³ãå®è¡ã®ãã¥ãŒãé 延ãããŠæ£ããæŽçããæ¹æ³ã¯ïŒ
JavaScriptã«ã¯ãã³ãŒãèªäœãå®è¡ãããDOMããªãŒãåæç»ãããã¿ã€ããŒãæ©èœãã1ã€ã®ã¹ã¬ããããããŸãã äžé£ã®æäœïŒãµã€ã¯ã«ãéãæ©èœïŒãå®è¡ãããã³ã«ãã€ã³ã¿ãŒãã§ã€ã¹ãšã®ãŠãŒã¶ãŒã€ã³ã¿ã©ã¯ã·ã§ã³ããããã¯ãããŸãïŒãã§ãŒã³ãéããªãå ŽåããŠãŒã¶ãŒã¯å€æŽã«æ°ã¥ããŸããïŒã UIã®ãããã¯ãé²ããããThreedã¯JavaScriptã«Web Workersã¹ã¬ãããå°å ¥ããŸããã
ã¯ãŒã«ãŒã䜿çšã§ããªãå Žåã¯ããµã€ã¯ã«ãšéãæ©èœãæé©åããå¿ èŠããããŸãã Nicholas C. ZakasãèæžOReilly High Performance JavaScriptã«æžããŠããããã«ãUI Threedã100ããªç§ä»¥äžã§ãããã¯ãããå ŽåããŠãŒã¶ãŒã¯é 延ã«æ°ä»ããªãã§ãããã ã€ãŸã 100ããªç§ãèšç®ã§ããã®ã§ããŠãŒã¶ãŒãé 延ã«æ°ä»ããªãããã«UI Threedã®ããã¯ã解é€ãã䟡å€ããããŸãã
以äžã¯ã圌ã®æ¬ã®ãã¹ãŠã®ããã»ããµãŒåãã«æé©åãããå ã®ã³ãŒãã§ãã
function timedProcessArray(items, process, callback) { var todo = items.concat(); //create a clone of the original setTimeout(function () { var start = +new Date(); do { process(todo.shift()); } while (todo.length > 0 && (+new Date() - start < 50)); if (todo.length > 0){ setTimeout(arguments.callee, 25); } else { callback(items); } }, 25); } function saveDocument(id) { var tasks = [openDocument, writeText, closeDocument, updateUI]; timedProcessArray(tasks, [id], function(){ alert("Save completed!"); }); }
timedProcessArray
é¢æ°ã¯ã25ããª
timedProcessArray
UI
timedProcessArray
ãããã¯ããäžé£ã®ã¢ã¯ã·ã§ã³ãå®è¡ããŠããã25ããª
timedProcessArray
UI
timedProcessArray
解æŸããŸãã
èªãïŒ
Nicholas C. Zakas-OReillyã®é«æ§èœJavaScript
Comix Web Workers
Web Workersã䜿çšããŠèšç®
WXHRïŒå³ã®ããå€ãè¯ãXHR
9.ãŠãŒã¶ãŒããŠã£ã³ããŠã®ãµã€ãºå€æŽãå®äºããããšãã©ãã«ãããŠç¥ãããšã¯å¯èœã§ããïŒ
ãã®ãããªã€ãã³ãã¯ãããŸãããããŠãŒã¶ãŒããã°ãããŠã£ã³ããŠã®ãµã€ãºãå€æŽãããã©ããã確èªã§ããŸããããã¯onresizeendã«ã»ãŒå¯Ÿå¿ããŠããŸãã
ã³ãŒãã¹ã±ããïŒ
var time = 0, timerId, TIME_ADMISSION = 100; // 0.1s function onresizeend () { console.log('onresizeend'); }; function resizeWatcher () { if (+new Date - time >= TIME_ADMISSION) { onresizeend(); if (timerId) { window.clearInterval(timerId); timerId = null; } } }; $(window).resize(function () { if (!timerId) { timerId = window.setInterval(resizeWatcher, 25); } time = +new Date; });
å®äŸïŒ jsfiddle.net/azproduction/2Yt6T
10. window.openïŒïŒã䜿çšããŠãã¿ãã§ã¯ãªãæ°ãããŠã£ã³ããŠãéãæ¹æ³
ãã®åäœã¯ãã©ãŠã¶åºæã§ãã Operaã¯åžžã«ã¿ããéããŸãïŒãã ãããŠã£ã³ããŠãšããŠè¡šç€ºãããŸãïŒãSafariã¯åžžã«ãŠã£ã³ããŠãéããŸãïŒSafariã®åäœã¯ãã€ãã¹ã§ããŸãïŒã ChromeãFFãããã³IEã¯å¶åŸ¡å¯èœã§ãã
è¿œå ã®ãã©ã¡ãŒã¿ãŒïŒãŠã£ã³ããŠã®äœçœ®ïŒãæž¡ããšãæ°ãããŠã£ã³ããŠãéããŸãã
window.open('http://www.google.com', '_blank', 'toolbar=0,location=0,menubar=0');
äœãéä¿¡ããªãå Žåãã¿ããéããŸãïŒ
window.open('http://www.google.com');
å€ãã®å Žåãæ°ããã¿ããéãå¿ èŠããããŸãããµãã¡ãªã«åé¡ãããå¯èœæ§ããããŸããããã©ã«ãã§ã¯ïŒèšå®ã«å¿ããŠïŒããµãã¡ãªã¯window.opené¢æ°ãåŒã³åºããããã³ã«æ°ãããŠã£ã³ããŠãéããŸãã ãã ãã
Ctrl+Shift/Meta+Shift
ãªãããªã³ã¯ãã¯ãªãã¯ãããšãïŒèšå®ã«é¢ä¿ãªãïŒåžžã«æ°ããã¿ããéããŸãã æ°ããã¿ããéãã«ã¯ã
Ctrl+Shift/Meta+Shift
ãŠãã¯ãªãã¯ãã€ãã³ãããšãã¥ã¬ãŒãããŸãã
function safariOpenWindowInNewTab(href) { var event = document.createEvent('MouseEvents'), mac = (navigator.userAgent.indexOf('Macintosh') >= 0); // Ctrl+Shift+LeftClick/Meta+Shift+LeftClick () // event.initMouseEvent( /* type */ "click", /* canBubble */ true, /* cancelable */ true, /* view */ window, /* detail */ 0, /* screenX, screenY, clientX, clientY */ 0, 0, 0, 0, /* ctrlKey */ !mac, /* altKey */ false, /* shiftKey */ true, /* metaKey */ mac, /* button */ 0, /* relatedTarget */ null ); // - $('<a/>', {'href': href, 'target': '_blank'})[0].dispatchEvent(event); }
11.å¹æçã«ãã£ãŒãã³ããŒãäœæããæ¹æ³ã¯ïŒ
oldObjectãå€æŽãããªãå Žåããããã¿ã€ããä»ããŠè€è£œããæ¹ãå¹ççã§ãïŒéåžžã«é«éã§ãïŒã
function object(o) { function F() {} F.prototype = o; return new F(); } var newObject = object(oldObject);
æ£çŽãªã¯ããŒã³äœæãå¿ èŠãªå Žåã¯ããªããžã§ã¯ãããªãŒãååž°çã«ãã©ããããã€ãã®æé©åãå®è¡ããæ¹ãé«éã§ãïŒãããŸã§ã®ãšããããããæéã®æ£çŽãªã¯ããŒã³äœæã¢ã«ãŽãªãºã ã§ãïŒã
var cloner = { _clone: function _clone(obj) { if (obj instanceof Array) { var out = []; for (var i = 0, len = obj.length; i < len; i++) { var value = obj[i]; out[i] = (value !== null && typeof value === "object") ? _clone(value) : value; } } else { var out = {}; for (var key in obj) { if (obj.hasOwnProperty(key)) { var value = obj[key]; out[key] = (value !== null && typeof value === "object") ? _clone(value) : value; } } } return out; }, clone: function(it) { return this._clone({ it: it }).it; } }; var newObject = cloner.clone(oldObject);
jQueryã®å Žåã次ã䜿çšã§ããŸãã
// var newObject = jQuery.extend({}, oldObject); // var newObject = jQuery.extend(true, {}, oldObject);
èªãïŒ
ãã³ãããŒã¯ã®æ£çŽãªã¯ããŒãã³ã°æè¡
stackoverflowã®äž»é¡ã«é¢ããéåžžã«é·ãè°è«
12.ã¢ããã°ãã¹ãã©ã¯ã¿/ãã¡ã€ãã©ã€ã¶ã®äœææ¹æ³ã¯ïŒ ãããŠäžè¬çã«ããªããžã§ã¯ãã®å¯¿åœã管çããæ¹æ³ã¯ïŒ
JavaScriptã§ã¯ããªããžã§ã¯ããžã®æåŸã®ãªã³ã¯ãæ¶ãããšããªããžã§ã¯ãã¯åé€ãããŸãã
var a = {z: 'z'}; var b = a; var c = a; delete az; delete a; // "" console.log(b, c); // : Object {} Object {},
ã€ãŸã ããã¹ãã©ã¯ã¿ãã䜿çšãããšããªããžã§ã¯ããå®å šã«åé€ããããšã¯ã§ããŸãã-å 容ãæ¶å»ããããšããã§ããŸããã
13.ãã€ããªããŒã¿ãåŠçããããšã¯å¯èœã§ããïŒ ãããããªããã©ã®ããã«ïŒ
JavaScriptã§ã¯ããã¹ãŠã®æ°å€ã¯æåå圢åŒã§äœ¿çšããããã«æ瀺ããããã€ããªããŒã¿ãæäœããããã®çµã¿èŸŒã¿ããŒã«ã¯ãããŸããã 2é²æ°ãæ±ãããã®BinaryParser JavaScriptã©ã€ãã©ãªããããŸãïŒãšã³ã³ãŒãããã³ãŒãïŒã³ãŒãã¯å°çã§ãïŒïŒ
ECMAScript 6+ïŒstrawmanïŒã«ã¯StructTypeã®ãã©ããããããŸãïŒããã¯ãC ++ãªã©ã§ããªãã¿ã®æ§é äœã§ãïŒã ãã€ããªãã¡ã€ã«ã®æäœãç°¡çŽ åããããã«å¿ èŠã§ãã å°æ¥çã«ã¯æ¬¡ã®ããã«ãªããŸãã
const Point2D = new StructType({ x: uint32, y: uint32 }); const Color = new StructType({ r: uint8, g: uint8, b: uint8 }); const Pixel = new StructType({ point: Point2D, color: Color }); const Triangle = new ArrayType(Pixel, 3); let t = new Triangle([{ point: { x: 0, y: 0 }, color: { r: 255, g: 255, b: 255 } }, { point: { x: 5, y: 5 }, color: { r: 128, g: 0, b: 0 } }, { point: { x: 10, y: 0 }, color: { r: 0, g: 0, b: 128 } }]);
donnerjack13589 ArtemS JavaScriptåä»ãé åã䜿çšããŠãããã¡ãŒããèªã¿åãããšãã§ããŸããã2é²æ°ãååŸããããšã¯ã§ããŸããã
XMLHttpRequestã¬ãã«2ã§ã¯ããã€ããªãã¡ã€ã«ãéåä¿¡ã§ããŸãã
14.ããé¢æ°ããå¥ã®é¢æ°ã®ã³ã³ããã¹ãå€æ°ãå€æŽããæ¹æ³ã¯ïŒ
1. smthã§ãã©ã€ããŒã³ã³ããã¹ããªããžã§ã¯ããžã®ãªã³ã¯ãæäŸã§ããŸãã
2.ãã©ã€ããŒã³ã³ããã¹ãã§çæãããé¢æ°ãsmthé¢æ°ã«æž¡ã
var primer = function (){ var a, b, c, d, e = {}; smth(function () { a = 1; b = 2; c = 3; d = 4; }, e); alert([a, b, c, d, e.pewpew]); }, smth = function (callback, e) { callback(); e.pewpew = "pewpew"; }; primer();
3.以åïŒFireFox 3.6-ïŒã¯__parent__ãä»ããŠã³ã³ããã¹ãã«å°éããããšãå¯èœã§ããããããŒãžã§ã³4ã§ã¯ãã®æ©èœã¯åé€ãããŸããã
15.èšäºãé¢æ°ãåŒã³åºã5ã€ã®æ¹æ³ãã«ã€ããŠã ãããã®Nã®æ¹æ³ã®ãã¡ã5ã€ç®ïŒèšäº4ãã³ã¡ã³ã4ãã³ã¡ã³ãæ°ïŒã®ã©ããé©çšãã¹ããããããŠãã®çç±ã¯ïŒ
ã°ããŒãã«ã³ãŒã«/ã¡ãœããã³ãŒã«ããã³ã³ã³ã¹ãã©ã¯ã¿ãŒã¯èæ ®ããŸããããããã®ã¹ã³ãŒãã¯ãã§ã«æ確ã§ãã
ç§ã¯é»è©±ã§åå¥ã«äœã¿ãç³è«ããŸãã ãããã¯åãããšãããŸã-æ瀺çãªthisã³ã³ããã¹ãã§é¢æ°ãåŒã³åºããŸãã
1.ã³ã³ã¹ãã©ã¯ã¿ãŒã®ãªãŒããŒã©ã€ããåŒã³åºããŠé©çšããŸãã
// function extend(newObj, oldObj) {function F() {}F.prototype = oldObj.prototype;newObj.prototype = new F();return newObj} var Obj = function () { this.obj_var = 100; }; Obj.prototype.obj_proto_var = 101; var NewObj = function () { Obj.call(this); // Obj Own property obj_var this.new_obj_var = 102; }; extend(NewObj, Obj) NewObj.prototype.new_obj_proto_var = 103; new NewObj(); // {new_obj_proto_var: 103, new_obj_var: 102, obj_proto_var: 101, obj_var: 100}
2.åŒæ°NodeListããã³ãã®ä»ã®é åã®ãããªãªããžã§ã¯ããé åã«å€æããã©ã€ããªã¹ãïŒgetElementsByTagNameïŒãé åã«å€æããŸã
// document.getElementsByTagName("div") ( ), document.getElementsByTagName("div").forEach(function (elem) { // ... }); // TypeError: document.getElementsByTagName("div").forEach is not a function // : slice this, Array.prototype.slice.call(document.getElementsByTagName("div")).forEach(function (elem) { // OK }); // Array.prototype.slice.call('pewpew') // ["p", "e", "w", "p", "e", "w"] // 8- undefined
3. Function.call.applyã䜿çšããŠã©ãããŒãäœæããããªãã¯ïŒ
ä»»æã®æ°ã®åŒæ°ã䜿çšããŠãæå®ãããã³ã³ããã¹ãã§barïŒïŒãåŒã³åºãã©ãããŒfooïŒïŒãèšè¿°ããå¿ èŠããããŸã
ãã€ãã«ã°ãã
åŸæ¥ã®åœ¢åŒã§ã¯ã次ã®ããã«ãªããŸãã
function bar() {} // foo(context, arg1, arg2, ...) function foo() { var context = arguments[0]; var args = Array.prototype.slice.call(arguments, 1); // bar bar.apply(context, args); }
ãã®ãµã©ãã®ä»£ããã«ãcall.applyããªãã¯ã䜿çšããŸãã
function foo() { Function.call.apply(bar, arguments); }
ããã¯æ¬¡ã®ããã«æ©èœããŸãïŒapllyã¯ãfooã«æž¡ããããã©ã¡ãŒã¿ãŒã䜿çšããŠbarãªããžã§ã¯ãã§Function.callãåŒã³åºããŸãã ã€ãŸããã³ã³ããã¹ããšarg1ãarg2ã䜿çšããæåã®äŸã§ã¯ã次ã®ããã«ãªããŸãã
bar.call(context, arg1, arg2)
4. ãã€ã³ããšãã¥ã¬ãŒã·ã§ã³
16.ããæ©èœã®å®è¡ç¯å²ãå¥ã®æ©èœã«ç§»ãæ¹æ³ã¯ïŒ
ãŸããã 以åïŒFireFox 3.6-ïŒã__ parent__ãä»ããŠã³ã³ããã¹ãã«å°éããããšã¯å¯èœã§ããããããŒãžã§ã³4ã§ã¯ãã®æ©èœã¯ã«ãããããŸããã
17.çŽæ¥åç §ãevalãããã³ãuse strictããªãã§ã°ããŒãã«ãªããžã§ã¯ããæ£ããååŸããã«ã¯ã©ãããã°ããã§ããïŒ
æ¡ä»¶ã®1ã€ãçç¥ããå ŽåããŸãã¯ã°ããŒãã«ã¹ã³ãŒãã§ã®ã¿å®è¡ããå Žåã次ã®ããšãã§ããŸãã
// 1: eval - on (function(){ "use strict"; var globalObject = (0, eval)("this"); // :) return globalObject; }()); // 2: - on (function(global){ // ... }(window)); // 3: "use strict" - off (function(){ return this; }()); // 4: , , . // "use strict"; (function(global){ // global })(this);
18.ã€ãã³ããã€ã³ã¿ãŒã»ããããåŸãjavascriptã§åŸã§åèµ·åããããšã¯å¯èœã§ããïŒ
ã€ãã³ãã«ã¯è² è·ããããããåãªãã€ãã³ãèšè¿°åã§ãã ãã ãã次ã®ããã«ãªã³ã¯ãã€ãã³ããã³ãã©ã«æ瀺çã«æž¡ãããšãã§ããŸãã
$('#smth').click(function onSmthClick(event){ if (smth) { // event.handlerFunction = onSmthClick; event.handlerContext = this; // // otherObjectSetSomeEvent event.handlerFunction otherObjectSetSomeEvent(event); } else { // - } });
ããããããã¯è¯ã解決çã§ã¯ãããŸããããªããªã ãŸã ããããšããããããããŸãã ãããŠãããžãã¯ã¯éåžžã«çŽããããã§ãã
ããžãã¯ãããçŽããæ±çšãã³ãã©ãŒã2ã€ã®éšåã«åå²ããããšããå§ãããŸãã
$('#smth').click(function handler1(event) { if (smth) { // leftObjectSetSomeEvent(event, function handler2(e) { // - event e }); } else { // - } }); function leftObjectSetSomeEvent(event, callback) { callback(event); // - event }
19.ãããŠãããªãã¯jsã§ããªãã®åèæžãæžããšã¯æããªãã£ãïŒ JavaScriptã®è©³çŽ°ãªåŠç¿å ŽæãæããŠãã ããã æ¬ããã¥ãŒããªã¢ã«ïŒ
å€ãã®ãµã€ããšå€ãã®æ¬ããããŸãããããã®äžéšã¯ãã·ã¢èªã«ç¿»èš³ãããŸãã ã
20. JSã§èŠçŽ ã®ããŒãžäžã®ãã¹ãŠã®ã¯ãªãã¯ãã€ã³ã¿ãŒã»ããããã«ã¯ã©ãããã°ããã§ããïŒ ã€ãŸããã·ã³ã°ã«ã¯ãªãã¯ãã³ãã©ãŒãäœæããŸã
DOMããªãŒã®äžçªäžã®ãªããžã§ã¯ãã«ã¯ãªãã¯ã€ãã³ããã³ãã©ãŒãæããå¿ èŠããããŸããèŠçŽ äžã®ãã¹ãŠã®ã¯ãªãã¯ã¯ããããã¢ãããããŸãïŒ
// jQuery $(window).bind('click', function (e) { console.log('Clicked on ', e.target); }); // - , jQuery delegate $('#pewpew').delegate('*', 'click', function (e) { console.log('Clicked on ', e.target); }); // $('#pewpew').delegate('.pewpew', 'click', function (e) { console.log('Clicked on element with .pewpew class name'); });
21. jQueryãªãã§XHRãå®è¡ããæ¹æ³ã¯ïŒ
éã¯ãã¹ãã©ãŠã¶ãŒæ©èœïŒ
function xhr(m,u,c,x){with(new XMLHttpRequest)onreadystatechange=function(x){readyState^4||c(x.target)},open(m,u),send()}
ã¯ãã¹ãã©ãŠã¶ãŒããããã«é·ãïŒ
function xhr(m,u,c,x){with(new(this.XMLHttpRequest||ActiveXObject)("Microsoft.XMLHTTP"))onreadystatechange=function(x){readyState^4||c(x)},open(m,u),send()}
䜿çšæ³ïŒ
xhr('get', '//ya.ru/favicon.ico', function(xhr){console.dir(xhr)});
22.ãã¡ã€ã«ã·ã¹ãã ã®æäœ
JavaScriptãä»ãããã¡ã€ã«ã·ã¹ãã ã®æäœãããšãã°ãã¡ã€ã«ã®èªã¿åããšæžã蟌ã¿ã«ã€ããŠåŠã³ãããšæããŸããã ã»ãšãã©ã®æç§æžã§ã¯ããã¡ã€ã«ã®æäœãäžèŠãªãã©ãŠã¶ãŒã®JavaScriptã«ã€ããŠèª¬æããŠããŸãããããšãã°ãµãŒããŒã¢ããªã±ãŒã·ã§ã³ãXULãªã©ã§ã¯ãããã¯å¿ èŠã§ãããããã¥ã¡ã³ãã¯ã»ãšãã©ãããŸããã
Habréã§ã®ãã¡ã€ã«ã¢ããããŒããå«ãããã¡ã€ã«ã·ã¹ãã ã§ã®ã¯ã©ã€ã¢ã³ãã®äœæ¥ã«ã€ããŠã¯ã次ã®ãããªèšäºããããŸãã
HTML5 File APIïŒãµãŒããŒãžã®è€æ°ã®ãã¡ã€ã«ã®ã¢ããããŒã
奜ã¿ãšãã³ãµãŒãå«ãhtml5 File APIã䜿çšããŠãã¡ã€ã«ãããŠã³ããŒããã
FileSystem APIãšFile APIïŒç解ããŠäœ¿çšãã
æçç»åã¢ããããŒããŒïŒ
Node.jsãµãŒããŒäžã®ãã¡ã€ã«ã®æäœã«é¢ããè¯ãèšäºããããŸãNode.jsã§ã®ãã¡ã€ã«ã®èªã¿åããšæžã蟌ã¿
XULã«ã€ããŠã¯ã MDC File_I / Oã®èšäºãã芧ãã ããã
23.ãªãããŒãåãã€ã³ããããã³ããããæå°åããæ¹æ³ã
1.ãã©ãŠã¶ã
requestAnimationFrame
é¢æ°ããµããŒãããŠããå Žåã
setInterval/setTimeout
代ããã«ããã䜿çšããå¿ èŠããã
setInterval/setTimeout
ãã©ãŠã¶ã¯ãåæã«å®è¡ãããã¢ãã¡ãŒã·ã§ã³ãæé©åããŠããªãããŒãšåãã€ã³ãã®åæ°ã1åã«æžããããšãã§ããŸããããã«ãããã¢ãã¡ãŒã·ã§ã³ã®ç²ŸåºŠãåäžããŸãã ããšãã°ãJavaScriptã®ã¢ãã¡ãŒã·ã§ã³ã¯ãCSSé·ç§»ãŸãã¯SVG SMILãšåæããŸãã ããã«ãã¢ãã¡ãŒã·ã§ã³ãé衚瀺ã®ã¿ãã§å®è¡ããããšããã©ãŠã¶ãŒã¯åæç»ãç¶ç¶ãããCPUãGPUãã¡ã¢ãªã®äœ¿çšéãå°ãªããªãããã®çµæãã¢ãã€ã«ããã€ã¹ã®ããããªãŒæ¶è²»ãåæžãããŸãã2.å€æ°ã®ãããŒãèŠçŽ ãé¿ããŸãïŒãªãããŒã¯æžå°ããŸãïŒ
4.ã§ããã ãDOMããªãŒãå€æŽããŸã-ã¡ã¢ãªã«æžã蟌ã¿ãDOMã«1åæ¿å ¥ããŸãïŒãªãããŒã¯æžå°ããŸãïŒ
5.ãã³ãã«å ã®ãªããžã§ã¯ãã®ããããã£ãå€æŽããŸãïŒãªãããŒãåæç»ã¯æžå°ããŸãïŒïŒããã¯ææ°ã®ãã©ãŠã¶ãŒã«ã¯åœãŠã¯ãŸããŸããïŒ
// element.style.left="150px;"; //... element.style.color="green"; // element.setAttribute('style', 'color:green;left:150px');
6.絶察ã«é 眮ããããªããžã§ã¯ãã®ã¿ã§ã¢ãã¡ãŒã·ã§ã³ãå®è¡ããŸãïŒãªãããŒã¯æžå°ããŸãïŒ
7.èŠçŽ ã®ã°ã«ãŒããå€æŽããåã«ã
style.display = "none"
é衚瀺ã«ã
style.display = "none"
ïŒãªãããŒã¯æžå°ããŸãïŒïŒããã¯ææ°ã®ãã©ãŠã¶ãŒã«ã¯åœãŠã¯ãŸããŸããïŒ
ãããã¯å€ã§ãããæé©åã«ã€ããŠãïŒ
8.ã€ãã³ãã®å§ä»»ã䜿çšããŠãã€ãã³ãã®æ°ãæžãããŸãã
9. DOMèŠçŽ ãžã®ãã£ãã·ã¥åç §ïŒã»ã¬ã¯ã¿ãŒã®åŒã³åºããæãã³ã¹ãã®ãããæäœã§ãïŒ
10.ã¯ã€ãã¯ã»ã¬ã¯ã¿ãŒé¢æ°querySelectorAllïŒïŒfirstElementChildã䜿çšããŸã
11. document.getElementsByTagNameã¯èŠçŽ ã®ãã©ã€ããã³ã¬ã¯ã·ã§ã³ãè¿ãããšã«æ³šæããŠãã ããïŒèŠçŽ ãDOMããªãŒã«è¿œå ããããšãã³ã¬ã¯ã·ã§ã³ã¯èªåçã«ãããåãåããŸãïŒ
æè¿ã®å€ãã®ãã©ãŠã¶ã§ã¯ããããã®æ¹æ³ã¯ãã®ãããªç®ã«èŠããå©ç¹ãäžããŸããïŒãã©ãŠã¶éçºè ã¯ãã¹ãŠãæé©åããŸãïŒã
èªãïŒ
Nicholas C. Zakas-OReillyã®é«æ§èœJavaScript
requestAnimationFrameã䜿çšããé«åºŠãªã¢ãã¡ãŒã·ã§ã³
24. childProcesses node.js - ?
ãªãŒããŒããããå€ãããããïŒããã¯Apacheã䜿çšããPHPã®ãããªãã®ã§ãïŒãäœåãªã¡ã¢ãªããã©ãŒã¯æéãåæåæéïŒjidã³ã³ãã€ã«ïŒãCPUè² è·ãªã©ãå²ãåœãŠããããåãªã¯ãšã¹ãã«å¯ŸããŠchildProcessesã䜿çšããªãã§ãã ãããNode.jsã¯è² è·ãéåžžã«ããåæ£ããã¢ããªã±ãŒã·ã§ã³ã®ã¡ã€ã³ã¹ã¬ããã§ãããã€ãã³ãåŠçã«ãŒããã§åäžã®ããã»ããµã³ã¢ãèªã¿èŸŒã¿ãŸããNode.jsã®çæ³çãªããŠã³ããŒãã¯ãã«ãŒãã«ããšã«1ãã©ãŒã¯ã§ããClusterã䜿çšããŠãã©ãŒã¯ããããšããå§ãããŸããã¯ã©ã¹ã¿ãŒã¯ãã©ã³ãµãŒïŒãã¹ã¿ãŒïŒãšããŠæ©èœãããã©ãŒã¯ïŒã¹ã¬ãŒãïŒã«ãªããŸãã倧éã®ãªã¯ãšã¹ãã«å¯ŸããchildProcessesã®äœ¿çšã¯æ£åœåãããŸãã
ããªãã¯ãŸã ããã§èªãããšãã§ããŸãïŒstackoverflow.com/questions/3491811/node-js-and-cpu-centric-requests
25. node.jsã§runInNewContextã䜿çšãã
runInNewContext? â node-js.ru/12-control-context-using-runinnewcontext
, ( Node.js â nodester). , â , . : / â GC ( ). .
ãããã«
TheShockã¯ããã®èšäºã«èšèŒãããŠããªããã¹ãŠã®è³ªåã«åçããŸãããéãã€ã³ã¿ãŒãã§ã€ã¹ïŒgmailãªã©ïŒã®ã¢ãŒããã¯ãã£ã«é¢ããèšäºããããŸãã
äžæãªç¹ãããå Žåã¯è³ªåããŠãã ããã