jquery源码01---(2880 , 3042) Callbacks : 回调对象 : 对函数的统一管理
// optionsCache : { 'once memory' : { once : true , memory : true } }
var optionsCache = {};
// once memory,options.match( core_rnotwhite )=[once, memory],function( _, flag )={once:true,memory:true}
function createOptions( options ) {
var object = optionsCache[ options ] = {};//中括号就是json的点,没有点了。
jQuery.each( options.match( core_rnotwhite ) || [], function( _, flag ) {
object[ flag ] = true;
});
return object;
}
/*
var cb = $.Callbacks();
function aaa(){
alert(1);
}
cb.add(aaa);
(function(){
function bbb(){
alert(2);
}
cb.add(bbb);
})();
cb.fire();//1 2
*/
jQuery.Callbacks = function( options ) {//类的静态方法
//options 可选: once memory unique stopOnFalse
//方法:add remove has empty disable disabled lock locked fireWith fire fired
//什么都不写走jQuery.extend( {}, options ),返回空{},不这样写,如果options===undefined,后面还要做兼容。
options = typeof options === "string" ?
( optionsCache[ options ] || createOptions( options ) ) :
jQuery.extend( {}, options );//options={once:true,memory:true}
var
memory,
fired,
firing,
firingStart,
firingLength,
firingIndex,
// 添加的所有方法
list = [],
stack = !options.once && [],
fire = function( data ) {
memory = options.memory && data;//有memory返回true
fired = true;//触发开始
firingIndex = firingStart || 0;
firingStart = 0;
firingLength = list.length;
firing = true;
for ( ; list && firingIndex < firingLength; firingIndex++ ) {
if ( list[ firingIndex ].apply( data[ 0 ], data[ 1 ] ) === false && options.stopOnFalse ) {// 函数返回false,有stopOnFalse属性就不向下执行
memory = false; //memory也要置为false
break;
}
}
firing = false;//触发结束
if ( list ) {
if ( stack ) {
if ( stack.length ) {
fire( stack.shift() );
}
} else if ( memory ) {
list = [];
} else {
self.disable();
}
}
},
self = {//对外部接口
add: function() {
if ( list ) {//list一上来是空数组,会返回真,
var start = list.length;
(function add( args ) {//args是形参
jQuery.each( args, function( _, arg ) {//遍历传进来的多个方法名,
var type = jQuery.type( arg );
if ( type === "function" ) {
if ( !options.unique || !self.has( arg ) ) {//不是unioqye,或者是unipue但是没有
list.push( arg );
}
} else if ( arg && arg.length && type !== "string" ) {//数组,嵌套
add( arg );
}
});
})( arguments );//arguments是实参,方法名aaa,
if ( firing ) {
firingLength = list.length;
} else if ( memory ) {//第一次没有赋值是undefined,
firingStart = start;
fire( memory );
}
}
return this;
},
remove: function() {
if ( list ) {
jQuery.each( arguments, function( _, arg ) {//可以删除多个
var index;
while( ( index = jQuery.inArray( arg, list, index ) ) > -1 ) {
list.splice( index, 1 );//删除
if ( firing ) {
if ( index <= firingLength ) {
firingLength--;
}
if ( index <= firingIndex ) {
firingIndex--;
}
}
}
});
}
return this;
},
has: function( fn ) {
return fn ? jQuery.inArray( fn, list ) > -1 : !!( list && list.length );
},
empty: function() {
list = [];
firingLength = 0;
return this;
},
disable: function() {
list = stack = memory = undefined;
return this;
},
disabled: function() {
return !list;
},
lock: function() {
stack = undefined;
if ( !memory ) {
self.disable();
}
return this;
},
locked: function() {
return !stack;
},
fireWith: function( context, args ) {
if ( list && ( !fired || stack ) ) {
args = args || [];
args = [ context, args.slice ? args.slice() : args ];
if ( firing ) {
stack.push( args );
} else {
fire( args );
}
}
return this;
},
fire: function() {
self.fireWith( this, arguments );//cb.fire('hello');
return this;
},
fired: function() {
return !!fired;
}
};
return self;
};
jquery源码01---(2880 , 3042) Callbacks : 回调对象 : 对函数的统一管理的更多相关文章
- jQuery源码逐行分析学习01(jQuery的框架结构简化)
最近在学习jQuery源码,在此,特别做一个分享,把所涉及的内容都记录下来,其中有不妥之处还望大家指出,我会及时改正.望各位大神不吝赐教!同时,这也是我的第一篇前端技术博客,对博客编写还不是很熟悉,美 ...
- jQuery源码逐行分析学习02(第一部分:jQuery的一些变量和函数)
第一次尝试使用Office Word,方便程度大大超过网页在线编辑,不过初次使用,一些内容不甚熟悉,望各位大神见谅~ 在上次的文章中,把整个jQuery的结构进行了梳理,得到了整个jQuery的简化结 ...
- jQuery源码解析资源便签
最近开始解读jQuery源码,下面的链接都是搜过来的,当然妙味课堂 有相关的一系列视频,长达100多期,就像一只蜗牛慢慢爬, 至少品读三个框架,以后可以打打怪,自己造造轮子. 完全理解jQuery源代 ...
- jQuery源码笔记(一):jQuery的整体结构
jQuery 是一个非常优秀的 JS 库,与 Prototype,YUI,Mootools 等众多的 Js 类库相比,它剑走偏锋,从 web 开发的实用角度出发,抛除了其它 Lib 中一些中看但不实用 ...
- jquery源码解析:代码结构分析
本系列是针对jquery2.0.3版本进行的讲解.此版本不支持IE8及以下版本. (function(){ (21, 94) 定义了一些变量和函数, jQuery = function() ...
- jQuery源码分析笔记
jquery-2.0.3.js版本源码分析 (function(){ (21,94) 定义了一些变量和函数 jQuery = function(){}; (96,283) 给JQ对象,添加一些方法 ...
- jQuery源码分析之整体框架
之前只是知道jQuery怎么使用,但是我觉得有必要认真的阅读一下这个库,在分析jQuery源码之前,很有必要对整个jQuery有个整体的框架概念,才能方便后面对jQuery源码的分析和学习,以下是我总 ...
- jquery源码分析(二)——架构设计
要学习一个库首先的理清它整体架构: 1.jQuery源码大致架构如下:(基于 jQuery 1.11 版本,共计8829行源码)(21,94) 定义了一些变量和函数jQu ...
- jQuery源码分析系列
声明:本文为原创文章,如需转载,请注明来源并保留原文链接Aaron,谢谢! 版本截止到2013.8.24 jQuery官方发布最新的的2.0.3为准 附上每一章的源码注释分析 :https://git ...
随机推荐
- 9-第一个app项目
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- java9新特性-5-Java的REPL工具: jShell命令
1.官方Feature 222: jshell: The Java Shell (Read-Eval-Print Loop) 2.产生背景 像Python 和 Scala 之类的语言早就有交互式编程环 ...
- OpenGL编程逐步深入(五)Uniform 变量
准备知识 在这个教程中我们会遇到一种新的Shader变量类型,即uniform变量.attribute(属性)变量和uniform变量的不同之处在于attribute 变量中包含顶点的具体数据,当每次 ...
- 仿函数(functor)
仿函数(functor),就是使一个类的使用看上去像一个函数.其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了. In computer programmin ...
- Linux cp 复制命令
Linux 的cp命令 功能: 复制文件或目录说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若 ...
- caffe(13) 数据可视化(python接口)配置
caffe程序是由c++语言写的,本身是不带数据可视化功能的.只能借助其它的库或接口,如opencv, python或matlab.大部分人使用python接口来进行可视化,因为python出了个比较 ...
- [NOIP2013提高组]货车运输
题目:洛谷P1967.Vijos P1843.codevs3287. 题目大意:有n个城市m条道路,每条道路有一个限重,规定货车运货不能超过限重.有一些询问,问你两个城市之间一次最多能运多少重的货(可 ...
- windows下用winscp的root连接ubuntu“拒绝访问”的解决方法
转载:https://www.cnblogs.com/weizhxa/p/10098640.html 解决: 1.修改ssh配置文件:sudo vim etc/ssh/sshd_config 在#Pe ...
- ConEmu windows上的终端工具
ConEmu Windows terminal 官网: http://conemu.github.io/
- caioj 1067动态规划入门(一维一边推5: 乘积最大(高精度版))
因为这里涉及到乘号的个数,那么我们可以用f[i][j]表示前i个位乘号为j个时的最大乘积 那么相比上一题就是多了一层枚举多少个乘号的循环,可以得出 f[i][r] = max(f[j - 1][r - ...