Jq source code parsing is bound to $, the method above jQuery, jqjquery

Source: Internet
Author: User

Jq source code parsing is bound to $, the method above jQuery, jqjquery

1. The method called directly with the $ symbol. How is jQuery encapsulated? Curious?

// The jQuery. extend method is bound to $. JQuery. extend ({// expando is used to determine the uniqueness of the current page. /\ D/non-numeric. The decimal point is removed. Expando: "jQuery" + (version + Math. random ()). replace (/\ D/g, ""), // Assume jQuery is ready without the ready module isReady: true, // error: function (msg) {throw new Error (msg) ;}, // null function noop: function () {}, // determine if it is a function isFunction: function (obj) {return jQuery. type (obj) = "function" ;}, // determines whether the current object is a window object. IsWindow: function (obj) {return obj! = Null & obj = obj. window;}, // determine whether obj is a number. When it is a numeric string, the page can be displayed. For example, "3.2" isNumeric: function (obj) {var type = jQuery. type (obj); return (type = "number" | type = "string ") & /// this means to restrict "3afc strings "! IsNaN (obj-parseFloat (obj);}, // judge whether obj is an object isPlainObject: function (obj) {var proto, Ctor; // obj exists and toString. call (obj )! = "[Object]"; it is definitely not an object. If (! Obj | toString. call (obj )! = "[Object Object]") {return false;} // getProto gets the object on the prototype chain. GetProto = Object. getPrototypeOf (); get the property proto = getProto (obj) on the prototype chain; // getProto (Object. create (null)-> proto = null this is also the Object obj = Object. create (null); if (! Proto) {return true;} // properties on the obj prototype. Proto has the constructor hasOwn = hasOwnPrototypeOf ('name') on it to determine whether an object itself has this attribute. // Ctor: When the proto itself has a constructor, the value of the constructor attribute is obtained. It is actually the obj constructor. Type-> function Ctor = hasOwn. call (proto, "constructor") & proto. constructor; // The Ctor type is "function" and it is the constructor type. In this case, obj is also an object. In my understanding, obj = new O (); is actually the real column return typeof Ctor = "function" & fnToString. call (Ctor) === ObjectFunctionString;}, // judge whether obj is an empty object isEmptyObject: function (obj) {// var o ={} var name; for (name in obj) {return false;} return true;}, // obtain the js data type. In fact, the method is Object. prototype. toString. call (xx); xx is a variable to be detected. The result is "[object]" "[object array]"... type: function (obj) {// remove null and undefined. Return. That is, null or undefined. Because undefined = null-> true. If (obj = null) {return obj + "" ;}// this corresponds to typeof xx (a variable)-> undefined object, number, string, function, boolean (typeof a variable can only get the data type in 6)/*** 1. if obj is an object or obj is a function, then class2type [toString. call (obj)] In class2type, find the corresponding value based on the key value. * Class2type = {* [object]: "object", * [object array]: "array. * Class2type [toString. call (obj)] | "object" is connected for read. If the type value cannot be found in class2type, object ** 2 is directly returned. or return typeof obj.. -> Number, string, and boolean are of the basic data type. (Js contains 5 basic data types. Null, undefined, number, string, boolean) */return typeof obj = "object" | typeof obj = "function "? Class2type [toString. call (obj)] | "object": typeof obj;}, // translates it into a global Eval function. Tell the truth. I didn't understand what this was about. DOMval ();/***** @ param code * function DOMEval (code, doc) {doc = doc | document; var script = doc. createElement ("script"); script. text = code; doc. head. appendChild (script ). parentNode. removeChild (script);} creates a script tag or removes it. I don't know how to use it. */GlobalEval: function (code) {DOMEval (code) ;}, // This is a function used to convert it into a camper. Ms-convert the prefix to the hump. CamelCase: function (string) {return string. replace (rmsPrefix, "ms-"). replace (rdashAlpha, fcamelCase) ;}, // each method. $. Each (obj, function () {}); method used to loop arrays and objects. Each: function (obj, callback) {var length, I = 0; if (isArrayLike (obj) {// execute this method length = obj when obj is an array. length; for (; I <length; I ++) {/* When $. each (obj, function (I, item) {if (I = 2) {return false. }) When the anonymous function in $. each (obj, function () {}) is pure return false;, the loop exists. */If (callback. call (obj [I], I, obj [I]) ===false) {break ;}} else {// for in loop object. Callback. call (obj [I], I, obj, [I]) = false is used together with the array loop for (I in obj) {if (callback. call (obj [I], I, obj [I]) === false) {break ;}} return obj ;}, // remove the white spaces on both sides of text $ ("input "). val (). trim. Text + "" is actually used to convert text into a string. Type. In this case, 123. replace (rtrim, "") will report an error. // If 123 + "" is actually "123" trim: function (text) {return text = null? "": (Text + ""). replace (rtrim, "") ;}, // $. makeArray is actually an array object to convert a class array. /***** @ Param arr * @ param results * @ returns {* | Array} * For example: var B = document. getElementsByTagName ("div"); B. reverse (). If you use B to call the reserver () method, an error is reported. In this case, B is a class array object. * Var a = $. makeArray (document. getElementsByTagName ("div"); a. reverser (). In this way, no error is reported. **/MakeArray: function (arr, results) {var ret = results | []; if (arr! = Null) {if (isArrayLike (Object (arr) {jQuery. merge (ret, typeof arr = "string "? [Arr]: arr);} else {push. call (ret, arr) ;}} return ret ;}, /***** @ param elem the value to be checked * @ param arr The array to be processed * @ param I queries from the nth digit of the array to be processed. the default value is 0 * @ returns {number}.-1 is returned. The value does not exist in arr, or the following table * $. inArray () of the value is displayed (). **/InArray: function (elem, arr, I) {// If arr is null,-1 is returned. /*** For indxOf. call (arr, elem, I); Method Description * var s = new String (); * eg: var indexOf = s. indexOf; Use the indexOf variable to store the indexOf method in the string. * IndexOf. call (arr, elem, I); in fact, it is to inherit the indexOf string to the array and pass the elem and I parameters. * A simpler one can be understood as: arr. indexOf (elem, I); */return arr = null? -1: indexOf. call (arr, elem, I );}, // merge arrays/***** @ param first array * @ param second array * @ returns {*} */merge: function (first, second) {var len = + second. length, // the length of the second array j = 0, // j starts from 0 I = first. length; // the length of the first array for (; j <len; j ++) {first [I ++] = second [j];} // actually, use push. First. length = I; return first;},/***** @ param elems with filter function * @ param callback filter add function * @ param invert to determine $. grep (arr, callback) returns an array that meets or does not meet the conditions. True is a condition. False does not meet the conditions. * @ Returns {Array} ** returns an Array. */Grep: function (elems, callback, invert) {var callbackInverse, matches = [], I = 0, length = elems. length, callbackct =! Invert; // Go through the array, only saving the items // that pass the validator function for (; I <length; I ++) {callbackInverse =! Callback (elems [I], I); if (callbackInverse! = Callbacksponct) {matches. push (elems [I]) ;}return matches ;}, /***** @ param elems array for processing * @ param callback function * @ param arg this parameter is used in the callback function. * Callback (elems [I], I, arg) * @ returns {*} ** $. map (arr, function (item, I, arg) {}, arg) * converts an array into another array through callback. * Eg: var B = [2, 3, 4]; * var a = $. map (B, function (item, I, arg) {* return item + arg; *}, 1) * console. log (a) [3, 4, 5] */map: function (elems, callback, arg) {var length, value, I = 0, ret = []; // Go through the array, translating each of the items to their new values if (isArrayLike (elems) {length = elems. length; for (; I <length; I ++) {value = callback (elems [I], I, arg); if (valu E! = Null) {ret. push (value) ;}// Go through every key on the object,} else {for (I in elems) {value = callback (elems [I], I, arg); if (value! = Null) {ret. push (value) ;}}// Flatten any nested arrays return concat. apply ([], ret) ;}, // a global flag of the object. Did not understand the specific use guid: 1, // Bind a function to a context, optionally partially applying any // arguments. /***** @ param fn * @ param context * @ returns {*} ** es6 also provides new Proxy (). Object. */Proxy: function (fn, context) {var tmp, args, proxy; // if (typeof context = "string") when content is a string ") {tmp = fn [context]; context = fn; fn = tmp;} // Quick check to determine if target is callable, in the spec // this throws a TypeError, but we will just return undefined. if (! JQuery. isFunction (fn) {return undefined;} // Simulated bind args = slice. call (arguments, 2); proxy = function () {return fn. apply (context | this, args. concat (slice. call (arguments) ;}; // Set the guid of unique handler to the same of original handler, so it can be removed proxy. guid = fn. guid = fn. guid | jQuery. guid ++; return proxy;}, // $. now current time rub now: Date. now, // jQuery. support is not used in Core but other projects attach their // properties to it so it needs to exist. /*** check whether the browser supports a certain attribute ** $. support. style */support: support });

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.