自己動手實作jQuery Callbacks完整功能程式碼詳解_jquery
用法和$.Callbacks完全一致, 但只是實現了add , remove , fire , empty, has和帶參數的構造函數功能, $.Callbacks 還有disable,disabled, fireWith , fired , lock, locked 方法
程式碼如下:
String.prototype.trim = function ()
{
return this.replace( /^\s+|\s+$/g, '' );
};
// Simulate jQuery.Callbacks object
function MyCallbacks( options )
{
var ops = { once: false, memory: false, unique: false, stopOnFalse: false };
if ( typeof options === 'string' && options.trim() !== '' )
{
var opsArray = options.split( /\s+/ );
for ( var i = 0; i < options.length; i++ )
{
if ( opsArray[i] === 'once' )
ops.once = true;
else if ( opsArray[i] === 'memory' )
ops.memory = true;
else if ( opsArray[i] === 'unique' )
ops.unique = true;
else if ( opsArray[i] === 'stopOnFalse' )
ops.stopOnFalse = true;
}
}
var ar = [];
var lastArgs = null;
var firedTimes = 0;
function hasName( name )
{
var h = false;
if ( typeof name === 'string'
&& name !== null
&& name.trim() !== ''
&& ar.length > 0 )
{
for ( var i = 0; i < ar.length; i++ )
{
if ( ar[i].name === name )
{
h = true;
break;
}
}
}
return h;
}
// add a function
this.add = function ( fn )
{
if ( typeof fn === 'function' )
{
if ( ops.unique )
{
// check whether it had been added before
if ( fn.name !== '' && hasName( fn.name ) )
{
return this;
}
}
ar.push( fn );
if ( ops.memory )
{
// after added , call it immediately
fn.call( this, lastArgs );
}
}
return this;
};
// remove a function
this.remove = function ( fn )
{
if ( typeof ( fn ) === 'function'
&& fn.name !== ''
&& ar.length > 0 )
{
for ( var i = 0; i < ar.length; i++ )
{
if ( ar[i].name === fn.name )
{
ar.splice( i, 1 );
}
}
}
return this;
};
// remove all functions
this.empty = function ()
{
ar.length = 0;
return this;
};
// check whether it includes a specific function
this.has = function ( fn )
{
var f = false;
if ( typeof ( fn ) === 'function'
&& fn.name !== ''
&& ar.length > 0 )
{
for ( var i = 0; i < ar.length; i++ )
{
if ( ar[i].name === fn.name )
{
f = true;
break;
}
}
}
return f;
};
// 逐一調用其中包含的函數
this.fire = function ( args )
{
if ( ops.once &&firedTimes > 0 )
{
返回此信息;
}
var r;
, ( var i = 0; i < ar.length; i )
if ( ops.stopOnFalse && r === false )
{
}
}
}
被解僱次數 ;
}
以這裡回來給
測試函數如下:(注意fn1 fn2是匿名函數,fn2回傳false,fn3是有「名稱」函數)
複製程式碼
程式碼如下:
return false;
};
function fn3( v ) {
🎜>
1 . 測試add & fire
var cb=new MyCallbacks();
cb.add(fn1)
cb.add(fn2)
cb.add(fn3)
cb.fire('hello')
輸出:
fn1 hello
fn2 hello
fn3 hello
2.測試remove
var cb=new MyCallbacks();
cb.add(fn1)
cb.add(fn2)
cb.add(fn3)
cb.remove(fn1)
cb.fire('hello')
cb.remove(fn3)
cb.fire('hello')
輸出:
fn1 hello
fn2 hello
fn3 hello
----------------------------
fn1 hello
fn2 hello
2.測試has
var cb=new MyCallbacks();
cb.add(fn1)
cb.add(fn2)
cb.add(fn3)
cb.has(fn1)
cb.has(fn3)
輸出:
false
---------------
true
3.測試帶參數的建構子 : once
var cb=new MyCallbacks('once')
cb.add(fn1)
cb.fire('hello')
cb.fire('hello')
cb.add(fn2)
cb.fire('hello')
輸出:
hello
-------------------
------------------
------------------------------
4.測試帶參數的建構子 : memory
var cb=new MyCallbacks('memory')
cb.add(fn1)
cb.fire('hello') // 輸出 : fn1 hello
cb.add(fn2) // 輸出 : fn2 hello
cb.fire('hello')
輸出 :
fn1 hello
fn2 hello
5.測試帶參數的建構子 : stopOnFalse
var cb=new MyCallbacks('stopOnFalse')
cb.add(fn1)
cb.add(fn2)
cb.add(fn3)
cb.fire('hello')
輸出:
fn1 hello
fn2 hello
6.測試帶參數的建構子 :unique
var cb=new MyCallbacks('unique')
b.add(fn3)
b.add(fn3)
cb.fire('hello')
輸出:
fn3 hello
7. 測試帶組合參數的建構子:四個設定參數可以隨意組合,一下只測試全部組合的情況, 不然要寫16個測試用例 T_T
var cb=new MyCallbacks('once memory unique stopOnFalse')
cb.add(fn1) // 輸出: fn1
cb.add(fn2) // 輸出: fn2
cb.add(fn3) // 輸出: fn3
cb.fire('hello')
輸出:
fn1 hello
fn2 hello
cb.fire('hello') // 輸出:沒有輸出
以下是官方API 文件:
Description: A multi-purpose callbacks list object that provides a powerful way to manage callback lists.The $.Callbacks() function is internally used to provide the base functionality behind the jeryDe $. ) components. It can be used as a similar base to define functionality for new components.
建構子 : jQuery.Callbacks( flags )
flags
Type: String
An optional list of space-separated flags that change how the callback list behaves.
Possible flags:
once: Encosurce the call list list like a Deferred).
memory: Keeps 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).<
stopOnFalse: Interrupts callings when a callback returns false.
By default a callback list will act like an event default a callback list will act like like 2000 月multiple times.
利用Callbacks 實現發布訂閱模式 pub/sub: (官方文件)
var 主題 = {};
jQuery.Topic = function ( id )
{
方法,
topic = id && topic[id];
{
topic = {
訂閱:callbacks.add,
取消訂閱:callbacks.remove
};
} {
為主題[id] = topic;
🎜> 回主體;
} ;
使用
複製程式碼
$.Topic( 'mailArrived' ).publish( { title: '郵件標題', content: '郵件內容' } );
實現了其餘的全部功能:callbacks.disable ,callbacks.disabled, callbacks.fired,callbacks.fireWith,callbacks.lock,callbacks.locked ,然後重構了下程式碼結構,將實現了暫停了匿名函數內,然後透過工廠方法window.callbacks 必須傳回實例,除非每次使用new 。
具體程式碼如下,有興趣和時間的可以對照jQuery版本的回調對比下:
複製程式碼
( function ( window, undefined )
{
// Simulate jQuery.Callbacks object
function Callbacks( options )
{
var ops = { once: false, memory: false, unique: false, stopOnFalse: false },
ar = [],
lastArgs = null,
firedTimes = 0,
_disabled = false,
_locked = false;
if ( typeof options === 'string' && options.trim() !== '' )
{
var opsArray = options.split( /\s+/ );
for ( var i = 0; i < options.length; i++ )
{
if ( opsArray[i] === 'once' )
ops.once = true;
else if ( opsArray[i] === 'memory' )
ops.memory = true;
else if ( opsArray[i] === 'unique' )
ops.unique = true;
else if ( opsArray[i] === 'stopOnFalse' )
ops.stopOnFalse = true;
}
}
function hasName( name )
{
var h = false;
if ( typeof name === 'string'
&& name !== null
&& name.trim() !== ''
&& ar.length > 0 )
{
for ( var i = 0; i < ar.length; i++ )
{
if ( ar[i].name === name )
{
h = true;
break;
}
}
}
return h;
}
// add a function
this.add = function ( fn )
{
if ( typeof fn === 'function' )
{
if ( ops.unique )
{
// check whether it had been added before
if ( fn.name !== '' && hasName( fn.name ) )
{
return this;
}
}
ar.push( fn );
if ( ops.memory )
{
// after added , call it immediately
fn.call( this, lastArgs );
}
}
return this;
};
// remove a function
this.remove = function ( fn )
{
if ( typeof ( fn ) === 'function'
&& fn.name !== ''
&& ar.length > 0 )
{
for ( var i = 0; i < ar.length; i++ )
{
if ( ar[i].name === fn.name )
{
ar.splice( i, 1 );
}
}
}
return this;
};
// remove all functions
this.empty = function ()
{
ar.length = 0;
return this;
};
// check whether it includes a specific function
this.has = function ( fn )
{
var f = false;
if ( typeof ( fn ) === 'function'
&& fn.name !== ''
&& ar.length > 0 )
{
for ( var i = 0; i < ar.length; i++ )
{
if ( ar[i].name === fn.name )
{
f = true;
break;
}
}
}
return f;
};
this.disable = function ()
{
_disabled = true;
return this;
};
this.disabled = function ()
{
return _disabled;
};
this.fired = function ()
{
return firedTimes > 0;
};
function _fire( context, args )
{
if ( _disabled || ops.once && firedTimes > 0 || _locked )
{
return;
}
if ( ar.length > 0 )
{
var r;
for ( var i = 0; i < ar.length; i++ )
{
r = ar[i].call( context, args );
if ( ops.stopOnFalse && r === false )
{
break;
}
}
}
firedTimes++;
if ( ops.memory )
{
lastArgs = args;
}
};
this.fireWith = function ( context, args )
{
context = context || this;
_fire( context, args );
return this;
};
this.fire = function ( args )
{
_fire( this, args );
return this;
};
this.lock = function ()
{
_locked = true;
return this;
};
this.locked = function ()
{
return _locked;
};
};
// ファクトリ メソッドとしてグローバルに公開
window.callbacks = function ( options )
{
return new Callbacks( options );
};
} )( window );

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

jQuery引用方法詳解:快速上手指南jQuery是一個受歡迎的JavaScript庫,被廣泛用於網站開發中,它簡化了JavaScript編程,並為開發者提供了豐富的功能和特性。本文將詳細介紹jQuery的引用方法,並提供具體的程式碼範例,幫助讀者快速上手。引入jQuery首先,我們需要在HTML檔案中引入jQuery函式庫。可以透過CDN連結的方式引入,也可以下載

jQuery中如何使用PUT請求方式?在jQuery中,發送PUT請求的方法與發送其他類型的請求類似,但需要注意一些細節和參數設定。 PUT請求通常用於更新資源,例如更新資料庫中的資料或更新伺服器上的檔案。以下是在jQuery中使用PUT請求方式的具體程式碼範例。首先,確保引入了jQuery庫文件,然後可以透過以下方式發送PUT請求:$.ajax({u

jQuery是一款廣泛應用於前端開發的快速、小巧、功能豐富的JavaScript庫。自2006年發布以來,jQuery已成為眾多開發者的首選工具之一,但在實際應用中,它也不乏一些優點和缺點。本文將深度剖析jQuery的優勢與劣勢,並結合具體的程式碼範例進行說明。優點:1.簡潔的語法jQuery的語法設計簡潔明了,可以大幅提升程式碼的可讀性和編寫效率。比如,

標題:jQuery小技巧:快速修改頁面所有a標籤的文字在網頁開發中,我們經常需要對頁面中的元素進行修改和操作。使用jQuery時,有時候需要一次修改頁面中所有a標籤的文字內容,這樣可以節省時間和精力。以下將介紹如何使用jQuery快速修改頁面所有a標籤的文本,同時給出具體的程式碼範例。首先,我們需要引入jQuery庫文件,確保在頁面中引入了以下程式碼:<

jQuery如何移除元素的height屬性?在前端開發中,經常會遇到需要操作元素的高度屬性的需求。有時候,我們可能需要動態改變元素的高度,而有時候又需要移除元素的高度屬性。本文將介紹如何使用jQuery來移除元素的高度屬性,並提供具體的程式碼範例。在使用jQuery操作高度屬性之前,我們首先需要了解CSS中的height屬性。 height屬性用於設定元素的高度

標題:使用jQuery修改所有a標籤的文字內容jQuery是一款受歡迎的JavaScript庫,被廣泛用於處理DOM操作。在網頁開發中,經常會遇到需要修改頁面上連結標籤(a標籤)的文字內容的需求。本文將介紹如何使用jQuery來實現這個目標,並提供具體的程式碼範例。首先,我們需要在頁面中引入jQuery庫。在HTML檔案中加入以下程式碼:

jQuery是一種流行的JavaScript庫,被廣泛用於處理網頁中的DOM操作和事件處理。在jQuery中,eq()方法是用來選擇指定索引位置的元素的方法,具體使用方法和應用場景如下。在jQuery中,eq()方法選擇指定索引位置的元素。索引位置從0開始計數,即第一個元素的索引是0,第二個元素的索引是1,依此類推。 eq()方法的語法如下:$("s

如何判斷jQuery元素是否具有特定屬性?在使用jQuery操作DOM元素時,常會遇到需要判斷元素是否具有某個特定屬性的情況。在這種情況下,我們可以藉助jQuery提供的方法來輕鬆實現這項功能。以下將介紹兩種常用的方法來判斷一個jQuery元素是否具有特定屬性,並附上具體的程式碼範例。方法一:使用attr()方法和typeof運算子//判斷元素是否具有特定屬
