1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
// 这里我们创建了一个单例模式 let single = function (fn) { let ret; return function () { return ret || (ret = fn.apply(this, arguments)); } };
let bindEvent = single(function () { // 虽然下面的renders函数执行3次,bindEvent也执行了3次 // 但是根据单例模式的特点,box上绑定的click事件只绑定了1次 // document.getElementById('box').onclick = function() { // alert('click'); // } document.getElementById('box').addEventListener('click', function () { console.log('click') }) return true; });
let renders = function() { console.log('渲染'); bindEvent(); }
renders(); renders(); renders();
|
近期评论