<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>jquery1.0.1</title> </head> <body> <div id="box">div#box</div> <script src="./jquery.1.0.2.js"></script> <script> console.log($('<a>')) console.log($('#box')) /* init 0: a length: 1 __proto__: Object init 0: div#box context: document length: 1 selector: "#box" __proto__: Object */ console.log($(document)) /* init 0: document context: document length: 1 */ </script> </body> </html>
( Function (the root) { var testExp = / ^ \ S * (<[\ W \ W is] +>) [^>] * $ / ; var rejectExp = / ^ <(\ W +) \ S * \ />? (:? <\ / \ L> |) $ / ; var Version = '1.0.1' var the jQuery = function (Selector, context) { return new new jQuery.prototype.init (Selector, context) } the jQuery.fn = the jQuery = .prototype { length: 0, // Merge using ??? ... the jQuery: Version, Selector: '', // pass over the call parameters when making objects, functions, string the init: function(Selector, context) { // context to limit the scope of the query context context = || Document; var match, elem, index = 0 ; IF (! Selector) { // $ () | $ (undefine) | $ (null) | $ (fasle) return the this ; } IF ( typeof Selector === 'String') { // two purposes, 1, 2 ,, creating a DOM node, querying the DOM node IF (selector.charAt (0) === '<' && selector.charAt (-selector.length. 1) === '>' && selector.length> =. 3) { // Selector is html string match = [Selector] // stored string html } IF (match) { // create a DOM node // merge array object (with a length property) [context.createElement (parse [1] )] (DOM node) jQuery.merge ( the this , jQuery.parseHTML (Selector, context) ); } the else { // querying the DOM node elem = document.querySelectorAll (Selector); // array- var the elems = Array.prototype.slice.call (elem); // into an array the this .length = elems.length; for (; index <elems.length; index ++ ) { the this [index] = the elems [index]; } the this .context = context; // instance object to the attribute context jquery extended the this .selector = selector; // / jquery extended to the object instance attribute selector } } the else IF (selector .nodeType) { // there nodeType, the object is passed over the this | Document | window the this .context = the this [0] = Selector; the this .length =. 1 ; return the this ; } the else IF ( typeof== Selector 'function') { // function // ? ? ? } }, } JQuery.fn.init.prototype = the jQuery.fn; // shallow copy, deep copy (first parameter to true) jQuery.fn.extend = jQuery.extend = function () { // The parameters SUMMARY and the number of implemented var target = arguments [0] || {} var length = The arguments.length; var I =. 1 ; var Deep = to false ; var Option, name, Copy, the src, copyIsArray, clone; IF ( typeof=== target 'boolean') { // determines whether copy depth identification, if it is a boolean Deep = target; // Deel copy identification target = arguments [. 1]; // to be assigned to the copied object second parameter I = 2; // to traverse the second parameter starts } IF ( typeof ! target == 'Object') { // the first parameter is not an object, the object is null give him assignment target = {} } IF (I === length) { // the number of parameters is determined, if the number of parameters is 1, it is jquey / jquery extension object instances of objects, target = this ; // refer to this object just i-- } // shallow copy for (; I <length; I ++) { // if the number of parameters is 1, beginning from the second parameter, if it is 0, beginning from the first one, a useless consumption IF ((Option ! = arguments [I]) = null ) { for (name in Option) { Copy = Option [name] the src = target [name]; IF (Deep && (jQuery.isPlainObject (Copy) || (copyIsArray = jQuery.isArray (copy)))) { // deep copy IF (copyIsArray) { copyIsArray = to false ; clone= src && jQuery.isArray(src)? src :[]; }else{ clone = src && jQuery.isPlainObject(src)? src :{}; } target[name] = jQuery.extend(deep,clone,copy) }else if(copy != undefined){ //浅拷贝 target[name] = copy; } } } } return target } jQuery.extend({ isPlainObject: function (obj) { return toString.call (obj) === '[Object Object]' }, the isArray: function (obj) { return toString.call (obj) === '[Object aArray]' }, / * * * * @param {First} - jQuery instance object => the this * * @param {SECOND} - array rEFERENCE => [DOM] * / merge: function (First, SECOND) { // merge array var L = second.length, // . 1 I = first.length, // 0 J = 0; IF ( typeof L == 'Number' ) { for (; J <L; J ++) { // for adding first array, to create a storage node dom first [I ++] = SECOND [J] // 0: 'A' the console.log (First); } } the else { } first.length = I; return First; }, ParseHTML: function (Data, context) { // parse html elements IF ! (Data || typeof ! Data = 'String') { Return null ; } // filter out '<a>' => 'A' var the parse = rejectExp.exec (Data); // with the tag name extracted regular return [context.createElement (the parse [. 1])]; // create a DOM element, the element is stored in the array, the array is returned to } }) root. $ = root.jQuery = jQuery }) ( the this )