var Class = function (parent) { var klass = function () { this.init.apply(this, arguments); } if (parent) { /** * 所有的子类都共享了同一个原型 * 避免在继承类的时候创建实例,只有实例的属性才会被继承 * 而非类的属性,设置对象的__proto__ * **/ var ctor = function () { }; ctor.prototype = parent.prototype; klass.prototype = new ctor; } klass.prototype.init = function () { } //prototype别名 klass.fn = klass.prototype; //定义类的别名 klass.fn.parent = klass; klass.__super__ = klass.__proto__; //给类添加属性 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;}var Animal = new Class;Animal.include({ breath:function () { console.log("breath") }})var Cat = new Class(Animal)var tommy = new Cat;console.log(tommy)