jQueryを勉強中。ソースコードを読んでいるけど、読めない。
/*! * jQuery JavaScript Library v1.3.1 * http://jquery.com/ * * Copyright (c) 2009 John Resig * Dual licensed under the MIT and GPL licenses. * http://docs.jquery.com/License * * Date: 2009-01-21 20:42:16 -0500 (Wed, 21 Jan 2009) * Revision: 6158 */
最初の"!"は何?
(function(){ var // Will speed up references to window, and allows munging its name. window = this, // Will speed up references to undefined, and allows munging its name. undefined, // Map over jQuery in case of overwrite _jQuery = window.jQuery, // Map over the $ in case of overwrite _$ = window.$, jQuery = window.jQuery = window.$ = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' return new jQuery.fn.init( selector, context ); }, // A simple way to check for HTML strings or ID strings // (both of which we optimize for) quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$/, // Is it a simple selector isSimple = /^.[^:#\[\.,]*$/;
お尻↓
});})();
なぜ、(function(){})();という書き方になっている??{}の中身だけ書けばいいのではないだろうか。スコープが変わってくる?うーむ。なんでだ。
var window = this のthisって、windowのことか?
var window = this,undefined,_jQuery = window.jQuery,...って、何で1文で書こうとしているんだ?それぞれの変数に対して個別にvarを使って宣言しないのはなぜ?改行入れてるんだったら、個別にvar宣言した方が読みやすいんじゃないかな。どんな意図があるんだろう。
undefinedっていう変数を宣言できるのか。
jQuery = window.jQuery = window.$ = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' return new jQuery.fn.init( selector, context ); },
jQuery.fn.init.prototypeってないのかな。あった↓。532行目。
// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;
しかも、jQuery.fn=jQuery.prototypeなんだよな。ということは、jQueryのすぺてのプロパティ、メソッドは、jQuery.fnに定義されていて、しかも、そのプロパティ、メソッドは、new jQueryで、jQueryオブジェクトにも定義される。さらに、new jQuery.fn.initで、、、う、わからん。これでなんのオブジェクトができるんだ? jQuery.fn.initオブジェクト?だめだ、わからなくなった。
うーむ。new jQuery(selector, context)としたら、return new jQuery.fn.init(selector, context)で作成されたオブジェクトってどうなるんだろう。
jQuery.fn = jQuery.prototype = { init: function( selector, context ) { // Make sure that a selection was provided selector = selector || document; // Handle $(DOMElement) if ( selector.nodeType ) { this[0] = selector; this.length = 1; this.context = selector; return this; }
initはreturnがある。new jQuery.fn.init(selector, context)を実行した時のreturnって、どういう意味があるんだろう。言い換えれば、コンストラクタがreturnを持っている場合って、そのreturnされたオブジェクトはどこへいく?そのオブジェクトがコンストラクタで作成されたことになる?うーむ。