首页 > 怎么基于jquery封装一个自己的框架?

怎么基于jquery封装一个自己的框架?

RT,领导让我把网站用到的javascript包装成框架,有什么思路吗?


搜索“制作jq插件”


封装成库吧,框架这东西太难了。简单点无脑就是吧所有逻辑全放在一个对象里面。
比如

(function($){
    var methods = {
        method1: function(options) {},
        method2: function() {},
        method3: function(args) {}
    };
    jQuery.fn.myObj = function(method) {
        if(methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if(typeof method === 'object' || !method) {
            return methods.method1.apply(this, arguments);
        } else {
            $.error('Method' + method + 'does not exist on jQuery.myObj');
        }
    }
})(jQuery);

复杂一点就要分层,核心代码Core,数据处理代码Data,UI等Interface分开处理。


推荐你看看这篇文章,很不错
javascript组件开发方式

【热门文章】
【热门文章】