回调函数

回调函数

开发工具与关键技术:VS JQ
作者:唐文坚
撰写时间:2020.9.18

callbacks.add(callbacks):
回调列表中添加一个回调或回调的集合。
Callbacks: 一个函数,或者一个函数数组用来添加到回调列表
JQ代码;
// a sample logging function to be added to a callbacks list
var foo = function( value ){
console.log( ‘foo:’ + value );
}

// another function to also be added to the list
var bar = function( value ){
console.log( ‘bar:’ + value );
}

var callbacks = $.Callbacks();

// add the function ‘foo’ to the list
callbacks.add( foo );

// fire the items on the list
callbacks.fire( ‘hello’ );
// outputs: ‘foo: hello’

// add the function ‘bar’ to the list
callbacks.add( bar );

// fire the items on the list again
callbacks.fire( ‘world’ );

// outputs:
// ‘foo: world’
// ‘bar: world’
callbacks.disable():
禁用回调列表中的回调,使用 callbacks.disable() 禁用回调列表:
代码:
// a sample logging function to be added to a callbacks list
var foo = function( value ){
console.log( value );
}

var callbacks = $.Callbacks();

// add the above function to the list
callbacks.add( foo );

// fire the items on the list
callbacks.fire( ‘foo’ ); // outputs: foo

// disable further calls being possible
callbacks.disable();

// attempt to fire with ‘foobar’ as an argument
callbacks.fire( ‘foobar’ ); // foobar isn’t output
callbacks.empty():
从列表中删除所有的回调.
使用 callbacks.empty() 清空回调列表:
代码:
// a sample logging function to be added to a callbacks list
var foo = function( value1, value2 ){
console.log( ‘foo:’ + value1 + ‘,’ + value2 );
}

// another function to also be added to the list
var bar = function( value1, value2 ){
console.log( ‘bar:’ + value1 + ‘,’ + value2 );
}

var callbacks = $.Callbacks();

// add the two functions
callbacks.add( foo );
callbacks.add( bar );

// empty the callbacks list
callbacks.empty();

// check to ensure all callbacks have been removed
console.log( callbacks.has( foo ) ); // false
console.log( callbacks.has( bar ) ); // false
callbacks.fire(arguments):
禁用回调列表中的回调,arguments:这个参数或参数列表传回给回调列表。
使用 callbacks.fire() 用任何已传递的参数调用列表中的回调:
代码
// a sample logging function to be added to a callbacks list
var foo = function( value ){
console.log( ‘foo:’ + value );
}

var callbacks = $.Callbacks();

// add the function ‘foo’ to the list
callbacks.add( foo );

// fire the items on the list
callbacks.fire( ‘hello’ ); // outputs: ‘foo: hello’
callbacks.fire( 'world '); // outputs: ‘foo: world’

// add another function to the list
var bar = function( value ){
console.log( ‘bar:’ + value );
}

// add this function to the list
callbacks.add( bar );

// fire the items on the list again
callbacks.fire( ‘hello again’ );
// outputs:
// ‘foo: hello again’
// ‘bar: hello again’

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值