We examine famous frameworks and libraries,such as jQuery,to experience the open source education.This article mainly analyses the structure of jQuery.
Course link: JavaScript: Understanding the Weird Parts
(Section 8: Examining Famous Frameworks and Libraries)
jQuery Introduction
jQuery is just a JavaScript library.What jQuery does essentially is let you manipulate the DOM.It handles the browser quirks but doesn’t add any features to the browser or to JavaScript itself.
the jQuery
function isn’t a function constructor.It’s just a function that returns an object,a function that returns a call to function constructor.
1 | // Define a local copy of jQuery |
jQuery.fn
is just an alias of jQuery.prototype
which is just hanging out(just an empty object at fisrt) and is overwritten with a new object.
jQuery.extend
and jQuery.fn.extend
are the some function.This function takes properties and methods of one object and adds them to another.
jQuery extends itself with many useful functions,such as map
,each
,type
,isArray
,makeArray
,and so on.
Sizzle
Sizzle
(http://sizzlejs.com/) is a whole other engine inside of jQuery.
A pure-JavaScript CSS selector engine designed to be easily dropped in to a host library.
The jQuery.extend
invocation is inside an IIFE,and the property Sizzle
is returned by another IIFE that returns a Sizzle object.That is to say,one IIFE can be inside another.
jQuery is using that Sizzle library.
1 | jQuery.find = Sizzle; |
init and jQuery prototype
jQuery.fn.init
is the function constructor.At the end of the function constructor,it’s returning a value,still giving back the empty object that was created with the new
operator,but doing some stuff to it first.
1 | init = jQuery.fn.init = function( selector, context ) { |
As for any new object created with the init
function constructor, its prototype is the same memory spot as jQuery.prototype
–the prototype of the jQuery object itself.
1 | //reminder: jQuery.fn = jQuery.prototype |
The neat trick:
- you don’t hava to call the
new
operator when you use jQuery.Instead this calls a function that then calls a function constructor,creating a new object.But that new object has access to all the properties and methods injQuery.fn
. - And instead of always in this jQuery code typing in jQuery.fn.init.something,it’s just the prototype of the jQuery object itself.
Method Chaining
method chaining: calling one method after another,and each method affects the parent object.
When you execute a method or call a method,and it’s found down the prototype chain,the this
variable points at the originating object,the object that actually made the call.
e.g. Just see the return value of addClass
function:
1 | addClass: function( value ) { |
Expose Globally
Expose jQuery or $
globally:
1 | // Expose jQuery and $ identifiers, even in |
Both of window.jQuery
and window.$
are pointing at the same spot in memory as the jQuery object created inside this function that had all these properties and methods attached to it.