JQueryã®ã½ã¼ã¹ã³ã¼ããèªãã§JavaScriptãåå¼·ï¼
æè¿JavaScriptããããæ©ä¼ãå¤ããããã¡ãã£ã¨åå¼·ããªãã¨ãããªããªã¨æã£ã¦ãã¾ãããªã®ã§ãjQuery(1.7.1)ã®ã½ã¼ã¹ã³ã¼ããèªãã§ã¿ã¾ããã
èªãã§æã£ãã¨ãããã¾ã¨ãã¦ã¿ããã¨æãã¾ããåå¼·ãããã¨ã®ã¾ã¨ãã§ãã
jQueryã®å ¨ä½å
(function( window, undefined ){ // Use the correct document accordingly with window argument (sandbox) var document = window.document, navigator = window.navigator, location = window.location; var jQuery = (function() { // (çç¥) // jQuery ã®æ¬ä½ã®functionãä½ã£ã¦returnãã })(); // (çç¥) // jQueryã®ãããããªæ©è½ã®å®è£ window.jQuery = window.$ = jQuery; // (3è¡åãããçç¥) })( window );
åºæ¬éª¨æ ¼ã¯ãããªã¨ããã§ããããï¼å ¨ä½ãç¡åé¢æ°ã«ãªã£ã¦ãã¦ãç´å¾ã«å¼ã³ããã¨ãããJavaScriptã®ã決ã¾ãã®ãã¿ã¼ã³ã«ãªã£ã¦ãã¾ãã
ç¡åé¢æ°ã« window ã渡ãã¦ãå¼æ°windowã§åãã¦ããã®ã§ãå¦çæ¬ä½ä¸ã®ãwindowãã¯å¼æ°ã®windowã«ãªã£ã¦ãã¾ãã第2å¼æ°ã¯ã渡ããã¦ããªãã®ã§ãå¼æ°undefinedã¯undefinedã¨ãªãã¾ãã
æå³ãå®å ¨ã«ã¯ããããªãã£ãã®ã§ããã¾ã説æã§ããªãã§ããããã®ããããèªãã ããªãã¨ãªãç´å¾ãã¾ããã
- http://d.hatena.ne.jp/gnarl/20110213/1297523372
- http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1040179045
ãã®å¾ããã¼ã«ã«å¤æ°jQueryã«æ©è½ãå®è£ ãã¦ãã£ã¦ãæå¾ã«jQueryãwindow.jQueryãwindow.$ ã«ä»£å ¥ãã¦ãjQueryãå ¬éãã¦ãã¾ãããã®ããããå®ç³ã ã¨æãã¾ãããã°ãã¼ãã«ã®åå空éã¸ã®å½±é¿ãæå°éã«ããæ¹æ³ã¨ãã¦åèã«ãªãã¾ãã
jQuery.extendã«ããæ§é å
jQuery.extend({ noConflict: function( deep ) { //(çç¥) }, isReady: false, //(çç¥) });
jQuery.extend({ Deferred: function( func ) { //(çç¥) });
jQuery.fn.extend({ //(çç¥) });
ã²ãããjQuery.extendãããã¦ãjQuery.fn.extendã§ããã³ã¢ã¨ãªãjQueryã®ãªãã¸ã§ã¯ããä½ã£ã¦(jQuery.extendãå«ã¾ãã)ãããã«æ©è½ã追å ãã¦ããã¨ããå®è£ ã«ãªã£ã¦ããããã§ãã
æ©è½ã®ã¾ã¨ã¾ããã¨ã«æ§é åããã¦ããã®ã§ãç§ã¿ãããªäººã§ãã9000è¡ãããã®ã½ã¼ã¹ã³ã¼ãã®å¿ è¦ãªç®æã ãèªãã¨ãã§ãããã§ãã
(9000è¡ãããèªãã¨ãè¨ããã¦ãã¾ãããã§ãã><ãããã¯ç½®ãã¨ãã¦ãå¿ è¦ãªç®æã ãèªãã°ããããã«ãã¦ãããã¨ã¯å¤§åãªäºã ã¨æãã¾ãã)
JavaScriptã®è«çæ¼ç®å
RubyãPHPã¨éã£ã¦JavaScriptã«ã¯è©³ãããªãã®ã§ãåºæ¬çãªãã¨ã§ããã¯ããã¦ç¥ãã¾ãããJavaScriptã¯ãããããã¡ããã¨è¨èªã®åå¼·ããªãã¨ãããªãæãã«ãªã£ã¦ãã¾ããã
0 || 1
ã¯1ã¨ãã¦è©ä¾¡ããã¾ããtrueã§ã¯ããã¾ããã
æ¼ç®åã¯ãã¾ãå·¦å´ãè©ä¾¡ããã¦ãtrueãªãã°å·¦å´ã®å¤ãè¿ãã¾ããfalseãªãã°ãå³å´ã®å¤ãè©ä¾¡ããã¦è¿ããã¾ãã |
ããã¯ãRubyã¨ä¸ç·ã§ãã^^ãã¡ãªã¿ã«ãPHPã¯||æ¼ç®åã¯booleanãè¿ãã¾ãã
PHPã®ãã¨ã
$foo = $foo ? $foo : 'defaultå¤';
ã¨ããããªãã¦ã
foo = foo || 'defaultå¤';
ã¨ãã§ããã®ã§ä¾¿å©ã§ãã
ã¡ãªã¿ã«ãRubyã ã¨ã
foo ||= 'defaultå¤'
ã§ã*1ãçãã§ããã
callã¨ãapplyã¨ã使ãããªããæ°ããªã
toString = Object.prototype.toString, hasOwn = Object.prototype.hasOwnProperty, push = Array.prototype.push, slice = Array.prototype.slice, trim = String.prototype.trim, indexOf = Array.prototype.indexOf, //(çç¥) jQuery.fn = jQuery.prototype = { //(çç¥) toArray: function() { return slice.call( this, 0 ); }, //(çç¥) };
ãã®ãããã§ãcallã¨ãapplyã¨ã使ããã¦ã¾ãã
ã¨ããèªãã¨ãArrayã®æ©è½ã使ãã®ã«callã¨ã使ãã¨æ¸ãã¦ãã£ã¦ãã¾ãã«ã¨ããæãã§ããã
ãã®ãããã¯è¦åå¼·ã§ããé åã£ã½ããªãã¸ã§ã¯ããä½ããããªã£ãæããã®ãããã®ã話ãæãåºããã¨æãã¾ãã
split + each
jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " + "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " + "change select submit keydown keypress keyup error contextmenu").split(" "), function( i, name ) {
æååã+ã§é£çµãã¦æ¹è¡ãã¦ãã®ãè¦ãããã¦ããã§ããã
ããã¦ãRubyã®%wã便å©ã ã¨æ¹ãã¦æãã¾ããã
%w(blur focus focusin focusout load resize scroll unload click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select submit keydown keypress keyup error contextmenu).each do |name|
ã¨ããããã§ãJavaScriptã®è¨äºã«è¦ãããããRubyã®å¸æè¨äºã§ããã