jQuery.Callbacks源码解读
一、源码解读
/*
* Create a callback list using the following parameters:
*
* options: an optional list of space-separated options that will change how
* the callback list behaves or a more traditional option object
*
* By default a callback list will act like an event callback list and can be
* "fired" multiple times.
*
* Possible options:
*
* once: 确保回调列表仅只fire一次
will ensure the callback list can only be fired once (like a Deferred)
*
* memory: 在执行过fire后,保存之前fire时的参数,该参数会传递给在add中执行的最新添加的回调
will keep track of previous values and will call any callback added
* after the list has been fired right away with the latest "memorized"
* values (like a Deferred)
*
* unique: 确保在add操作中,阻止存在回调列表中的回调再次被添加到回调列表中
will ensure a callback can only be added once (no duplicate in the list)
*
* stopOnFalse: 当正在执行的回调返回false,将中断其他未执行回调的执行
interrupt callings when a callback returns false
*
*/
var optionsCache = {},
// Used for splitting on whitespace
core_rnotwhite = /\S+/g;
// Convert String-formatted options into Object-formatted ones and store in cache
function createOptions( options ) {
// optionsCache[ options ] 用于缓存 object所引用的值
var object = optionsCache[ options ] = {};
jQuery.each( options.match( core_rnotwhite ) || [], function( _, flag ) {
object[ flag ] = true;
});
return object;
}
jQuery.Callbacks = function( options ) {
// Convert options from String-formatted to Object-formatted if needed
// (we check in cache first)
options = typeof options === "string" ?
// 只有当执行$.Callbacks(参数相同)二次及以上时,才不会执行createOptions函数
( optionsCache[ options ] || createOptions( options ) ) :
// 说明也可以这样$.Callbacks({once:true, memory:true})使用
jQuery.extend( {}, options );
var // Flag to know if list is currently firing
firing,
// Last fire value (for non-forgettable lists)
memory,
// Flag to know if list was already fired
fired,
// End of the loop when firing
firingLength,
// Index of currently firing callback (modified by remove if needed)
firingIndex,
// First callback to fire (used internally by add and fireWith)
firingStart,
// Actual callback list
list = [],
// Stack of fire calls for repeatable lists
stack = !options.once && [],
// Fire callbacks
// data传递的是一个数组
// 使用Callbacks.fireWidth时,data包含fireWith函数传递的一个上下文环境和一个数组
// 使用Callbacks.fire时,data包含Callbacks对象和fire函数的arguments对象
fire = function( data ) {
// 如果options.memory为true,记录下data传递的数组
memory = options.memory && data;
fired = true;
// 如果options.memory为true,firingStart为上一次Callbacks.add后回调列表的length值
firingIndex = firingStart || 0;
// 重置firingStart为0
firingStart = 0;
firingLength = list.length;
firing = true;
for ( ; list && firingIndex < firingLength; firingIndex++ ) {
// 将data作为参数,执行回调列表中的所有回调
// 如果回调列表中其中一个回调返回false,且options.stopOnFalse为true,则中断接下来其他回调的执行
// 如果options.memory为true,将memory设置为false,阻止在Callbacks.add中新增回调的执行
if ( list[ firingIndex ].apply( data[ 0 ], data[ 1 ] ) === false && options.stopOnFalse ) {
memory = false; // To prevent further calls using add
break;
}
}
firing = false;
if ( list ) {
// 如果options.once为false
&n
补充:web前端 , JavaScript ,