// 分段代码 function Class() { } Class.prototype.construct = function() {};

Class.asMethod = function(func, superClass) {
return function() { var currentSuperClass = this.$; this.$ = superClass; var ret = func.apply(this, arguments);
this.$ = currentSuperClass; return ret; }; };

Class.extend = function(def) { var classDef = function() { if (arguments[0] !== Class) { this.construct.apply(this, arguments); } };

var proto = new this(Class);
var superClass = this.prototype;

for (var n in def) {
    var item = def[n];                        
    
    if (item instanceof Function) {
        item = Class.__asMethod__(item, superClass);
    }
    
    proto[n] = item;
}

proto.$ = superClass;
classDef.prototype = proto;

//赋给这个新的子类同样的静态extend方法
classDef.extend = this.extend;        
return classDef;

};

// ES6代码 class Class { constructor() {} static asMethod(func, superClass) {
return function() { let currentSuperClass = this.$; this.$ = superClass; let ret = func.apply(this, arguments);
this.$ = currentSuperClass; return ret; }; } static extend(def) { let classDef = function() { if (arguments[0] !== Class) { this.construct.apply(this, arguments); } }; let proto = new this(Class); let superClass = this.prototype; for (let n in def) { let item = def[n]; if (item instanceof Function) { item = Class.asMethod(item, superClass); } proto[n] = item; } proto.$ = superClass; classDef.prototype = proto; classDef.extend = this.extend; return classDef; } construct() {}

分段以下代码并转换为es6代码function Class Classprototypeconstruct = function ;Class__asMethod__ = functionfunc superClass return function var currentSuperClass = this$; this$ = superClass;

原文地址: https://www.cveoy.top/t/topic/fiuT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录