mass Framework oop模块 v4
将oop模块永久更名为class模块,移除一些nonew,unnew这两个配置属性,想使用无new实例化可以通过如下方法实现:
dom(function(){ var test = function(name){ return new test.fn.init(name) } dom.mix(test, dom["class"]).include({ init:function(name){ console.log("=============") this.name = name; }, getName:function(){ return this.name }, setName:function(name){ this.name = name } }); test.fn = test.prototype; test.fn.init.prototype = test.fn; var a = test("司徒正美") alert(a.getName()) });
新增setOptions API.
在Prototype中,我们重新一个类的属性,通常使用如下的格式:
this.options = { method: 'post', asynchronous: true, contentType: 'application/x-www-form-urlencoded', encoding: 'UTF-8', parameters: '', evalJSON: true, evalJS: true }; Object.extend(this.options, options || { });
在jquery中,可能就是这样,从理论上来使用深拷贝应该更安全,不过这也视情况而定。
this.options = { method: 'post', asynchronous: true, contentType: 'application/x-www-form-urlencoded', encoding: 'UTF-8', parameters: '', evalJSON: true, evalJS: true }; this.options = $.extend(true,{}, this.options, options );
我的框架与mootools走的是同一路线,专门提供一个方法干这事情:
dom.require("class",function(){ var A = dom.factory({ options:{aaa:"1111"}, init:function(){ this.setOptions({ aaa:"aaa", bbb:"bbb", ccc:"ccc" }); } }); var a = new A; dom.log(a.options) });
//by 司徒正美 2011.7.4 ;(function(global,DOC){ var dom = global[DOC.URL.split("#")[0]]; dom.define("class", "lang",function(){ //========================================= // 核心模块 第一类工厂 //========================================== var P = "prototype", C = "constructor", F = "function", EXTEND = [P, 'extend', 'include', 'inherit', 'ancestors', 'parent'], INCLUDE = [C], classOne = dom.oneObject(['Object',"Array","Function"]), objectEach = dom.Object2.forEach; function addModule(klass,props){ 'extend,include'.replace(/\w+/g, function(name){ var modules = props[name]; if(classOne[dom.type(modules)]){ klass[name].apply(klass,[].concat(modules)); delete props[name]; } }); } function cleanModule(module,props){ for(var i = 0, name ; name = props[i++];){ delete module[name] } return module; } dom["class"] = { inherit : function(parent) { if (parent && parent[P]) { this[P] = Object.create(parent[P]);//高效设置原型链 this.parent = parent; } this.ancestors = []; while (parent) {//收集所有父类,用于构建方法链时查找同名方法 this.ancestors.push(parent); parent = parent.parent; } this[P].setOptions = function(){ this.options = dom.Object2.merge.apply(this.options || {}, arguments) return this; } return this[P][C] = this; }, extend: function(){//扩展类成员 for(var i = 0, module; module = arguments[i++]; ){ if(module){ dom.mix(this, cleanModule(module,EXTEND)) } } return this; }, include:function(){//扩展原型成员 var parents = [this].concat(this.ancestors), target = this[P],modules = []; for(var i = 0, module; module = arguments[i++]; ){ if(dom.type(module,"Object")){ modules.push(module); }else if(typeof module === F){ modules.push(new module); } } modules.forEach(function(module){ objectEach.call(cleanModule(module,INCLUDE),function(method,name){ var i = 0,parent,super_method; while((parent = parents[i++])){ if (parent[P] && name in parent[P]) { super_method = parent[P][name]; break; } } if( typeof method === F && typeof super_method === F ){ target[name] = function() { this.$super = super_method; return method.apply(this, arguments); } target[name].toString = dom.K(method + ""); }else{ target[name] = method; } }); }); return this; } }; dom.factory = function(obj){ obj = obj || {}; var parent = obj.inherit || {}; //父类 var init = obj.init || function(){}; delete obj.inherit; delete obj.init; var klass = function() { parent.apply && parent.apply(this, arguments); return init.apply(this, arguments);//确保有实例返回 }; dom.mix(klass,dom["class"]).inherit(parent ).extend(parent ); addModule(klass,obj);//添加obj.extend,obj.include里面的对象或类到类中去 klass.toString = dom.K(init+""); return klass.include(obj); } }); })(this,this.document);
机器瞎学/数据掩埋/模式混淆/人工智障/深度遗忘/神经掉线/计算机幻觉/专注单身二十五年