这是装饰者模式。。话说有点蒙var decorator = function ( input , fn ) { // 获取事件源
var input = document.getElementById(input); // 若事件源是已经绑定的事件
if ( typeof input.onclick === 'function' ) { // 缓存事件原有的回调函数
var oldClickFn = input.onclick; // 为事件源添加新的事件
input.onclick = function () { // 事件源原有回调函数
oldClickFn(); // 执行事件源新增回调函数
fn();
}
} else { // 事件源未绑定事件,直接为事件源添加新增回调函数
input.onclick = fn;
}
}; // 电话输入框功能装饰
decorator( 'tel_input' , function () { document.getElementById( 'tel_demo_text' ).style.display = 'none' ;
} )这函数怎么理解呢?为什么执行了oldClickFn();还要执行fn();?
添加回答
举报
0/150
提交
取消