var Class = function(){ var klass = function(){ this.init.apply(this,arguments); } klass.prototype.init = function(){} klass.fn = klass.prototype; klass.fn.parent = klass; //对象的属性直接复制到类上 klass.extend = function(obj){ var extended = obj.extended; for(var i in obj){ klass[i] = obj[i]; } if(extended) extended(klass) } //对象的属性复制到类的原型中 klass.include = function(obj){ var included = obj.included; for(var i in obj){ klass.fn[i] = obj[i]; } if(included) included (klass) } return klass; }
使用extend()函数来生成一个类,这个函数的参数是一个对象。通过迭代将对象的属性直接复制到类上:
var Person = new Class; Person.extend({ find:function(){}, exists:function(){} }); var person = Person.find(1);
《基于MVC的Javascript》
时间: 2024-10-01 00:23:08