分析以下代码说出其所用开发语言添加每段代码的注释及用es6优化function Class Classprototypeconstruct = function ;Classextend = functiondef var classDef = function if arguments0 !== Class thisconstructapplythis argumen
所用开发语言:JavaScript
注释及es6优化:
//定义一个空的Class构造函数 function Class() { }
//给Class构造函数的原型对象添加一个construct方法 Class.prototype.construct = function() {};
//给Class构造函数添加extend方法 Class.extend = function(def) { //定义一个新的构造函数classDef //如果传入的参数arguments[0]不是Class,则调用该构造函数的construct方法 const classDef = function() { if (arguments[0] !== Class) { this.construct.apply(this, arguments); } };
//创建一个新的原型对象proto,继承自Class的原型对象
const proto = Object.create(this.prototype);
//获取父类的原型对象
const superClass = this.prototype;
//遍历传入的def对象,将每个属性和方法添加到proto对象上
for (let n in def) {
const item = def[n];
if (item instanceof Function) {
//将方法的$属性指向父类的原型对象
item.$ = superClass;
}
proto[n] = item;
}
//将proto对象赋值给classDef的原型对象
classDef.prototype = proto;
//赋给这个新的子类同样的静态extend方法
classDef.extend = this.extend;
return classDef;
}
原文地址: https://www.cveoy.top/t/topic/fivw 著作权归作者所有。请勿转载和采集!