2012年9月16日星期日

【代码组织】♣一

【代码组织】♣一

<!DOCTYPE html><html lang="zh-CN"><head>    <meta charset="utf-8" />    <title></title></head><body>    <script>        // ...减少全局变量的策略,例如命名空间模式或是函数立即自动执行        (function () {            var a = 1;            alert(a);// 1        } ());// 推荐使用这个 http://www.cnblogs.com/TomXu/archive/2011/12/31/2289423.html        (function () {            var a = a || 3;            alert(a);// 3        })();        alert(a);// a is not defined    </script></body></html>

深入理解JavaScript系列(3):全面解析Module模式
http://www.cnblogs.com/TomXu/archive/2011/12/30/2288372.html
(原)原生js封装的焦点图(幻灯片)效果一
http://www.cnblogs.com/hongru/archive/2010/09/23/1833441.html
归纳成此一种:

<!DOCTYPE html><html lang="zh-CN"><head>    <meta charset="utf-8" />    <title></title>    <style type="text/css">        span{color:#aa0;font:100px/1.5 arial}    </style></head><body>    <span id="time">0:0:0</span>    <input id="start" type="button" value="start" />    <input id="stop" type="button" value="stop" />    <input id="clear" type="button" value="clear" />    <script>        var W$ = function (el) {            return document.getElementById(el);        };        var Woogle = {};        Woogle.timer = function () {            var ss;            return {                init: function (options) {                    this.elTime = W$(options.time);                    this.auto(this.elTime);                },                auto: function (el) {                    var h, m, s;                    this.a = setInterval(function () {                        ss ? ss += 1 : ss = 1;                        h = Math.floor(ss / 3600) % 24;                        m = Math.floor(ss / 60) % 60;                        s = ss % 60;                        el.innerHTML = h + ':' + m + ':' + s;                    }, 1000);                },                start: function () {                    this.auto(this.elTime);                },                stop: function () {                    clearInterval(this.a);                },                clear: function (el) {                    ss = 0;                    el.innerHTML = '0:0:0';                    clearInterval(this.a);                }            };        } ();        Woogle.timer.init({            time: 'time'        });        (function () {            var elStart = W$('start'),                elStop = W$('stop'),                elClear = W$('clear');            elStart.onclick = function () {                Woogle.timer.start();            };            elStop.onclick = function () {                Woogle.timer.stop();            };            elClear.onclick = function () {                Woogle.timer.clear(Woogle.timer.elTime);            };        } ());    </script></body></html>

TAG: