JavaScript 학습 노트 15 규범 인 코딩

1610 단어 JavaScript
//        
    var hook = (function () {
        return {
            timer:null,
            init:function () {
                this.callHooks('init');
            },
            callHooks:function (init) {
                var s = "hook_" + init + '_event',
                        f = [];
                for (var h in window) {
                    if (h.indexOf(s) != 0) continue;
                    f.push(h);
                }
                this.hooksTimeout(f);
            },
            hooksTimeout:function (hooks) {
                if (0 === hooks.length) {
                    if (this.timer) clearTimeout(this.timer);
                    return;
                }

                var h = hooks.shift();
                window[h].apply();
                window[h] = undefined;

                window.setTimeout(function () {
                    hook.hooksTimeout(hooks);
                }, 200);
            }
        }
    }());

    //   1              ,           
    var hook_init_event_tpl_html = function () {
        document.getElementById('test').innerHTML = 'This is HTML!';
    }

    //   2              ,           
    var hook_init_event_tpl_console = function () {
        console.log('This is console!');
    }

    //             ,    window.onload()  
    hook.init();

좋은 웹페이지 즐겨찾기