(function( window, undefined ) { var document = window.document, navigator = window.navigator, location = window.location; [...] // window.jQuery = window.$ = jQuery; })(window);
var jQuery = (function () { var jQuery = function ( selector, context ) { return new jQuery.fn.init( selector, context, rootjQuery ); }; // Map over jQuery in case of overwrite _jQuery = window.jQuery, // Map over the $ in case of overwrite _$ = window.$, // A central reference to the root jQuery(document) rootjQuery, [...] rootjQuery = jQuery(document); [...] return jQuery; })();
jQuery.noConflict()
) as well as some rootjQuery - a jQuery object with a link to the document (cache of the frequently encountered $(document)
, optimization)''.trim
and [].indexOf
, because jQuery retains references to them and uses native implementations in its jQuery.trim
and jQuery.inArray
. trim = String.prototype.trim, indexOf = Array.prototype.indexOf,
var jQuery = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' return new jQuery.fn.init( selector, context, rootjQuery ); },
jQuery.fn.init
" is created and returned. This place uses Javascript magic. Just below the code we can find something like the following: jQuery.fn = jQuery.prototype = { constructor: jQuery, init: function( selector, context, rootjQuery ) { [...] } [...] } // Give the init function the jQuery prototype for later instantiation jQuery.fn.init.prototype = jQuery.fn;
jQuery.fn
is nothing more than a jQuery
prototype and this knowledge will help us to figure out something lower. Also, jQuery.fn.init.prototype
points to the jQuery
prototype, and the constructor jQuery.fn.init.prototype
points to jQuery
. This approach gives us a very interesting result. Open the jQuery Chrome console and enter: $(document) instanceof jQuery; // true $(document) instanceof jQuery.fn.init; // true
var Init = function () { console.log('[Init]'); }; var jQuery = function () { console.log('[jQuery]'); return new Init(); }; Init.prototype = jQuery.prototype = { constructor: jQuery }; var $elem = jQuery(); // [jQuery] , [Init] console.log( $elem instanceof jQuery ); // true console.log( $elem instanceof Init ); // true
jQuery.fn.init
object jQuery.fn.init
, and jQuery
is the jQuery.fn.init
object jQuery.fn.init
$(function () { alert('READY!') }); // , DOM $(document.getElementById('test')); // $('<div />'); // $('<div />', { title: 'test' }); // // css-: $('#element'); // "element" $('.element', $previous ); // element $previous $("div[name=city]:visible:has(p)"); // ,
jQuery.fn.init
. I will give here pseudocode : init: function( selector, context, rootjQuery ) { if ( !selector ) return this; // Handle $(DOMElement) if ( selector.nodeType ) return this = selector; // The body element only exists once, optimize finding it if ( selector === "body" && !context ) return this = document.body; if ( jQuery.isFunction( selector ) ) { return rootjQuery.ready( selector ); } // Handle HTML strings if ( typeof selector === "string" ) { // Verify a match, and that no context was specified for #id if ( selector.match(quickExpr) ) { if ( match[1] ) { return createNewDomElement( match[1] ); } else { return getById( match[2] ) } } else { return jQuery( context ).find( selector ); } } },
quickExpr = /^(?: [^<]*(<[\w\W]+>)[^>]*$ | #([\w\-]*)$ )/;
find
method is called from the current context, which searches for an element using the search engine (also JResig authorship) Sizzle
(rights belong to The Dojo Foundation ). var MyClass = function () { // constructor }; MyClass.prototype = { // prototype }; var instance = new MyClass(); // , MyClass.prototype.plugin = function () { console.log("He's alive!"); }; instance.plugin(); // He's alive!
jQuery.prototype.plugin = function () { // Here is my plugin };
fn
is a short link to jQuery.prototype
, so you can write shorter: jQuery.fn.plugin = function () { // Here is my plugin // this jquery-, };
jQuery.plugin = function () { // Here is my plugin };
new function (document, $, undefined) { var privateMethod = function () { // private method, used for plugin }; $.fn.myPlugin = function () { }; // , , dom-: $.myPlugin = function () { }; }(document, jQuery);
css
vs. setStyles
, attr
vs. setAttributes
, etc. The idea was simply beautiful and captivated the minds of many. Very often jQuery clones are found or ideas are transferred to other languages. But simplicity is deceptive. And it is not always good, so always think three times, before shortening the clear name of your method, it may come out sideways for you;)Source: https://habr.com/ru/post/118564/
All Articles