前端业务代码规范

<script>
    (function (global,$,_,doc) {
        var report=function (options) {

            this.eventsMap={
               'click #btn1':'btn1click',
               'click #btn2':'btn2click',
            };

            this.initializeElements();
            this.initialization();
        };
        
        report.Eles={
            a:"#a",
            b:"#b",
            c:"#c"
        };

        report.prototype={
            constructor:report,
            initializeElements:function () {
                var eles=report.Eles;
                for(var name in eles)
                {
                    if(eles.hasOwnProperty(name))
                    {
                        this[name]=$(eles[name]);
                    }
                }
               //  console.log(this.a.html());
               // console.log(this['a'].html());
            },

            initialization:function ()
            {
                this.bindEvent(this.eventsMap);
            },

            _scanEventsMap:function (maps,isOn) {
                var delegateEventSplit=/^(\S+)\s*(.*)$/;
                var bind=isOn ? this._delegate:this._undelegate;
                for(var keys in maps)
                {
                    if(maps.hasOwnProperty(keys))
                    {
                        var matches=keys.match(delegateEventSplit);
                        bind(matches[1],matches[2],this[maps[keys]].bind(this));
                    }
                }
            },
            initializeOrdinaryEvents:function (maps) {
                this._scanEventsMap(maps, true);
            },
            uninitializeOrdinaryEvents: function(maps) {
                this._scanEventsMap(maps);
            },
            _delegate:function (name,selector,func) {
                doc.on(name,selector,func);
            },
            _undelegate:function (name,selector,func) {
                doc.off(name,selector,func);
            },
            bindEvent:function (maps) {
                this.initializeOrdinaryEvents(maps);
            },
            unbindEvent: function(maps) {
                this.uninitializeOrdinaryEvents(maps);
            },
            destroy: function() {
                this.unbindEvent();
            },
            btn1click:function () {
                console.log("11111111111111");
            },
            btn2click:function () {
                console.log("222222222222");
            }
        };
        //暴露接口
        global.report=report;

        $(function () {
            new report();
        })
    }(this,this.jQuery,this._,this.jQuery(document)));
</script>

 

posted @ 2017-04-09 21:22  layfork  阅读(262)  评论(0编辑  收藏  举报