var initializing = false, fnTest = /xyz/.test(function() { xyz; }) ? /\b_super\b/ : /.*/; //构造基类 this.jaClass = function() {}; jaClass.extend = function(props){ var _super = this.prototype; initializing = true; var prototype = new this(); initializing = false; for (var name in props){ //这一段是赋值到prototype中,运用三元运算符的方式很灵活 //满足函数的条件作为函数插入到prototype中 prototype[name] = typeof props[name] =="function" && typeof _super[name] == "function" && fnTest.test(props[name]) ? (function(name, fn) { return function(){ var tmp = this._super; this._super = _super[name]; var ret = fn.apply(this, arguments); this._super = tmp; return ret; }; })(name, props[name]) : props[name] } function Class(){ if(!initializing && this.ctor) this.ctor.apply(this, arguments); } //子类prototype指向父类的实例,继承的关键 Class.prototype = prototype ; Class.prototype.constructor = Class; //子类自动获得extend方法,arguments.callee 指向当前正在执行的函数 Class.extend = arguments.callee; return Class; };