從大四下學期開始瞭解jquery源碼相關的東西,在回校參加畢業典禮(準確的說是參加補考掛科太多)期間便開始藉着《jQuery》內幕學習jquery源碼,而後在博客園寫筆記也已經兩個月了,也寫了十幾篇筆記了可是感受本身仍是懂了jquery的九牛一毛,做爲一名初學者而言,但願經過jquery來增長本身js的能力,可是真正作起來確實難的,自己jquery寫的很深奧,再加上日常工做也很忙,中間又辭職找工做找房子住等等就致使學習斷斷續續的,其實我寫的時候很出東西本身也是很困惑的一直但願有人給指點迷津可是彷佛你們沒太多熱情在jqeury源碼上面,做爲一個前端開發人員要學習的東西太多了可能你們不想把時間浪費在它上面吧。哈哈,有點跑題了,下面開始正題:html
大體就是這麼多還有ready方法是屬於dom加載模塊的放到後面,如今回過頭看看其實東西也不是不少嘛,如今再把這個模塊的源碼貼出來對比着看一下前端
jQuery.fn = jQuery.prototype = { constructor: jQuery, init: function( selector, context, rootjQuery ) { var match, elem, ret, doc; // Handle $(""), $(null), or $(undefined)
if ( !selector ) { return this; } // Handle $(DOMElement)
if ( selector.nodeType ) { this.context = this[0] = selector; this.length = 1; return this; } // The body element only exists once, optimize finding it
if ( selector === "body" && !context && document.body ) { this.context = document; this[0] = document.body; this.selector = selector; this.length = 1; return this; } // Handle HTML strings
if ( typeof selector === "string" ) { // Are we dealing with HTML string or an ID?
if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) { // Assume that strings that start and end with <> are HTML and skip the regex check
match = [ null, selector, null ]; } else { match = quickExpr.exec( selector ); } // Verify a match, and that no context was specified for #id
if ( match && (match[1] || !context) ) { // HANDLE: $(html) -> $(array)
if ( match[1] ) { context = context instanceof jQuery ? context[0] : context; doc = ( context ? context.ownerDocument || context : document ); // If a single string is passed in and it's a single tag
// just do a createElement and skip the rest
ret = rsingleTag.exec( selector ); if ( ret ) { if ( jQuery.isPlainObject( context ) ) { selector = [ document.createElement( ret[1] ) ]; jQuery.fn.attr.call( selector, context, true ); } else { selector = [ doc.createElement( ret[1] ) ]; } } else { ret = jQuery.buildFragment( [ match[1] ], [ doc ] ); selector = ( ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment ).childNodes; } return jQuery.merge( this, selector ); // HANDLE: $("#id")
} else { elem = document.getElementById( match[2] ); // Check parentNode to catch when Blackberry 4.6 returns
// nodes that are no longer in the document #6963
if ( elem && elem.parentNode ) { // Handle the case where IE and Opera return items
// by name instead of ID
if ( elem.id !== match[2] ) { return rootjQuery.find( selector ); } // Otherwise, we inject the element directly into the jQuery object
this.length = 1; this[0] = elem; } this.context = document; this.selector = selector; return this; } // HANDLE: $(expr, $(...))
} else if ( !context || context.jquery ) { return ( context || rootjQuery ).find( selector ); // HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
} else { return this.constructor( context ).find( selector ); } // HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) { return rootjQuery.ready( selector ); } if ( selector.selector !== undefined ) { this.selector = selector.selector; this.context = selector.context; } return jQuery.makeArray( selector, this ); }, // Start with an empty selector
selector: "", // The current version of jQuery being used
jquery: "1.7.1", // The default length of a jQuery object is 0
length: 0, // The number of elements contained in the matched element set
size: function() { return this.length; }, toArray: function() { return slice.call( this, 0 ); }, // Get the Nth element in the matched element set OR
// Get the whole matched element set as a clean array
get: function( num ) { return num == null ?
// Return a 'clean' array
this.toArray() : // Return just the object
( num < 0 ? this[ this.length + num ] : this[ num ] ); }, // Take an array of elements and push it onto the stack
// (returning the new matched element set)
pushStack: function( elems, name, selector ) { // Build a new jQuery matched element set
var ret = this.constructor(); if ( jQuery.isArray( elems ) ) { push.apply( ret, elems ); } else { jQuery.merge( ret, elems ); } // Add the old object onto the stack (as a reference)
ret.prevObject = this; ret.context = this.context; if ( name === "find" ) { ret.selector = this.selector + ( this.selector ? " " : "" ) + selector; } else if ( name ) { ret.selector = this.selector + "." + name + "(" + selector + ")"; } // Return the newly-formed element set
return ret; }, // Execute a callback for every element in the matched set.
// (You can seed the arguments with an array of args, but this is
// only used internally.)
each: function( callback, args ) { return jQuery.each( this, callback, args ); }, ready: function( fn ) { // Attach the listeners
jQuery.bindReady(); // Add the callback
readyList.add( fn ); return this; }, eq: function( i ) { i = +i; return i === -1 ?
this.slice( i ) : this.slice( i, i + 1 ); }, first: function() { return this.eq( 0 ); }, last: function() { return this.eq( -1 ); }, slice: function() { return this.pushStack( slice.apply( this, arguments ), "slice", slice.call(arguments).join(",") ); }, map: function( callback ) { return this.pushStack( jQuery.map(this, function( elem, i ) { return callback.call( elem, i, elem ); })); }, end: function() { return this.prevObject || this.constructor(null); }, // For internal use only.
// Behaves like an Array's method, not like a jQuery method.
push: push, sort: [].sort, splice: [].splice };
其中最後三個方法沒有說因他們原本就是Array對象的原型方法node
// Save a reference to some core methods
toString = Object.prototype.toString, hasOwn = Object.prototype.hasOwnProperty, push = Array.prototype.push, slice = Array.prototype.slice, trim = String.prototype.trim, indexOf = Array.prototype.indexOf,
昨天剛回到北京,由於要回去拿本身的畢業證,拿到了證書也就意味着大學生涯乃至學生生涯的結束,內心五味雜陳,從開始接觸網頁到大三決定作一名web開發人員種種經歷浮現眼前,想起上課的時候我在最後面偷偷寫代碼的情景,想起本身剛開始學習js熱情開心起來都忘記吃飯的熱情,想起用js寫俄羅斯方塊時由於要不停地調試按鍵盤點鼠標差點被自習室的其餘同窗趕出來情景.....那個時候常常被人家問你每天都在搞什麼?哈哈,也許將來我不會再作編程者 可是至少如今我還會一直努力着.......jquery