var exports = this; (function($){ var mod = {}; //返回Controller模型 mod.create = function(includes){ var result = function(){ this.init.apply(this, arguments); }; result.fn = result.prototype; result.fn.init = function(){}; result.proxy = function(func){ return $.proxy(func, this); }; result.fn.proxy = result.proxy; result.include = function(ob){ $.extend(this.fn, ob); }; result.extend = function(ob){ $.extend(this, ob); }; //将传入的对象扩展制模型 if(includes) result.include(includes); return result; }; exports.Controller = mod; })(jQuery); //在文档加载完毕后创建控制器 jQuery(function($){ var ToggleView = Controller.create({ init: function(view){ this.view = $(view); this.view.bind('mouseover',true, this.proxy(this.toggleClass)); this.view.bind('mouseout',false, this.proxy(this.toggleClass)); }, toggleClass: function(e){ this.view.toggleClass('over', e.data); } }); //实例化控制器, 调用init(); var tv = new ToggleView('#view'); console.log(tv); });
==================这里是分割线====================
这种方式创建的控制器模型,是在文档加载完毕后才初始化控制器