JQuery源码解析--callbacks

 (function (global, factory) {
     factory(global);
 })(this, function (window, noGlobal) {
     var rootjQuery;
     var class2type = {};
     var toString = class2type.toString;
     var arr = [];
     var indexOf = arr.indexOf;
     var myJQuery = function (selector, context) {
         return new myJQuery.fn.init(selector, context);
     };

     myJQuery.fn = myJQuery.prototype = {
         constructor: myJQuery,//为撒指定
         length: 0,
         each: function (callback) {
             return myJQuery.each(this, callback);
         },
         pushStack: function (elems) {
             var ret = myJQuery.merge(this.constructor(), elems);
             ret.prevObject = this;
             ret.context = this.context;
             return ret;
         }
     };

     myJQuery.extend = myJQuery.fn.extend = function () {
         var options, name, src, copy, target = arguments[0] || {}, length = arguments.length, i = 1;
         if (i === length) {
             target = this;
             i--;
         }
         for (; i < length; i++) {
             if ((options = arguments[i]) != null) {
                 for (name in options) {
                     src = target[name];
                     copy = options[name];
                     if (target === copy) {
                         continue;
                     }

                     if (copy !== undefined) {
                         target[name] = copy;
                     }
                 }
             }
         }
         return target;
     };

     function isArrayLike(obj) {
         var length = !!obj && "length" in obj && obj.length,
             type = myJQuery.type(obj);
         if (type === "function" || myJQuery.isWindow(obj)) {
             return false;
         }

         return type === "array" || length === 0 ||
             typeof length === "number" && length > 0 && ( length - 1 ) in obj;
     }

     myJQuery.extend({
         isWindow: function( obj ) {
             return obj != null && obj === obj.window;
         },
         //extend each,是一种静态方法 调用方式 myJQuery.each(.....)
         each: function (obj, callback) {
             var length, i = 0;
             if (isArrayLike(obj)) {
                 //如果是数组
                 length = obj.length;//设置的属性
                 for (; i < length; i++) {
                     if (callback.call(obj[i], i, obj[i]) === false) {
                         break;
                     }
                 }
             }
             else{
                 //如果是对象
                 for(i in obj){
                     if(callback.call(obj[i],i,obj[i])===false){
                         break;
                     }
                 }
             }
             return obj;
         },
         isFunction: function (obj) {
             return myJQuery.type(obj) === "function";
         },
         type: function (obj) {
             if (obj == null) {
                 return obj + "";
             }

             // Support: Android<4.0, iOS<6 (functionish RegExp)
             /*return typeof obj === "object" || typeof obj === "function" ?
              class2type[toString.call(obj)] || "object" :
              typeof obj;*/
             return typeof obj;
         },
         merge: function (first, second) {
             var len = +second.length,
                 j = 0,
                 i = first.length;
             for (; j < len; j++) {
                 first[i++] = second[j];
             }
             first.length = i;
             return first;
         },
         inArray: function (elem, arr, i) {
             return arr == null ? -1 : indexOf.call(arr, elem, i);
         }
     });

     myJQuery.fn.extend({
         find: function (selector) {
             var ret = [];
             var arr = [12, 35, 68];
             ret = this.pushStack(arr);
             ret.selector = selector;
             return ret;
         }
     });

     var rnotwhite = (/\S+/g); //大s 非空白字符 小s 空白字符
     ///g 表示该表达式将用来在输入字符串中查找所有可能的匹配,返回的结果可以是多个。如果不加/g最多只会匹配一个

     function createOptions(options) {
         var object = {};
         //match 匹配成功,返回数组,否则 返回null
         myJQuery.each(options.match(rnotwhite) || [], function (_, flag) {
             object[flag] = true;
         });
         return object;
     }

     myJQuery.Callbacks = function (options) {
         options = typeof  options === "string" ? createOptions(options) : myJQuery.extend({}, options);
         var firing, memory, fired, locked, list = [], queue = [], firingIndex = -1;
         var fire = function () {
             locked = options.once;
             for (; queue.length; firingIndex = -1) {
                 memory = queue.shift();
                 while (++firingIndex < list.length) {
                     if(list[firingIndex].apply(memory[0], memory[1])===false && options.stopOnFalse===true){
                         firingIndex=list.length;
                         memory = false; //预防 参数里有 memory
                     }
                 }
             }

             //memory是一个全局变量,一直存在,而在add的时候,又有
             // if(memory && !firing){ fire(); }
             if(!options.memory){
                 memory=false;
             }

             if (locked) {
                 if (memory) {
                     list=[];
                 } else {
                     list = "";
                 }
             }
         };
         var self = {
             add: function () {
                 if (list) {
                     if (memory && !firing) {
                         firingIndex = list.length - 1;
                         queue.push(memory);
                     }

                     (function addSelf(args) {
                         myJQuery.each(args, function (_, arg) {
                             if (myJQuery.isFunction(arg)) {
                                 if (!options.unique || !self.has(arg)) {
                                     list.push(arg);
                                 }
                             } else if (arg && arg.length && myJQuery.type(arg) !== "string") {
                                 //适用于 add([fn1,fn2]);
                                 addSelf(arg);
                             }
                         });
                     })(arguments);

                     if(memory && !firing){
                         //传入 memory关键字,在add的时候,将之前所有的fn fire 一次
                         fire();
                     }
                     return this;
                 }
             },
             remove: function () {
                 myJQuery.each(arguments, function (_, arg) {
                     var index = 0;
                     while ((index = myJQuery.inArray(arg, list, index)) > -1) {
                         list.splice(index, 1);
                     }
                 });
                 return this;
             },
             has: function (fn) {
                 return fn ? myJQuery.inArray(fn, list) > -1 : list.length > 0;
             },
             empty: function () {
                 if (list) {
                     list = [];
                 }
                 return this;
             },
             disable: function () {

             },
             disabled: function () {

             },
             lock: function () {

             },
             locked: function () {

             },
             fireWith: function (context, args) {
                 //when options.once=true,the locked=true,and not fired
                 if (!locked) {
                     args = args || [];
                     queue.push([context, args]);
                     fire();
                 }
                 return this;
             },
             fire: function () {
                 self.fireWith(this, arguments);
                 return this;
             },
             fired: function () {

             }
         };
         return self;
     }
     var init = myJQuery.fn.init = function (selector, context, root) {
         if (!selector) {
             return this;
         }
         root = root || rootjQuery;
         //处理标签 div p li
         if (typeof selector === "string") {
             return ( context || root ).find(selector);
         } else if (selector.nodeType) {
             this.context = this[0] = selector;
             this.length = 1;
             return this;
         }
     };

     init.prototype = myJQuery.fn;

     rootjQuery = myJQuery(document);

     if (!noGlobal) {
         window.myJQuery = window.F$ = myJQuery;
     }

     return myJQuery;
 });
上一篇:node-webkit教程<>Native UI API 之Menu(菜单)


下一篇:post 封装Map 发送请求