JavaScript 简易继承实现
2016-07-18 18:55
405 查看
最近发现写代码的效率超来超低了,花了两天的功夫,才写出这么一个段,凑合看看吧。
还算是比较简单吧,下面看看怎么用:
输出结果:
需要注意的有:
“__”:被当成私有对象处理,不会被继承
“___baseFunctionName”、“____currentFunctionName”被当成继承者信息存储属性,不能当其它使用了。
var FUNCTION_MARK_SEPARATOR = "__";//特殊分隔符 Function.prototype.bindFunction = function () { var self = this; var params = []; params.push.apply(params, arguments); var thisArg = params[0]; params = params.slice(1); return function () { var arr =[]; arr.push.apply(arr,params); arr.push.apply(arr,arguments); if(thisArg) { self.apply(thisArg, arr); }else{ self.apply(this,arr); } } } //基础对象执行者 function baseFunctionExcenter() { var params = []; params.push.apply(params, arguments); var self = this; var n = params[0]; var name = n; if (arguments.callee) { var caller = arguments.callee.caller;//func->bindFunction->baseFunctionExcenter if (caller && (caller = caller.caller) && caller.name == n) { caller = caller.caller; if (caller && caller.___baseFunctionName == n && caller.___currentFunctionName) { name = caller.___currentFunctionName; } else { name = n; } } } name = FUNCTION_MARK_SEPARATOR + name; var func = self[name]; if (func) { func.apply(self, params.slice(1)); } } function initClass(obj) { obj.super = function () { var superClass = arguments[0]; if (!superClass) return; var base = null; if (arguments.length > 1) { var F = function () { }; F.prototype = superClass.prototype; base = new F(); var params = []; params.push.apply(params, arguments); superClass.apply(base, params.slice(1)); } else { base = new superClass(); } var superResetList = [];//需要重置的对象 var include = {}; for (var n in base) { var obj = base ; this.super = obj; if ("super" == n) continue; //去掉没有必要继承的对象 if (typeof (n) == "string" && n.length >= FUNCTION_MARK_SEPARATOR.length && FUNCTION_MARK_SEPARATOR == n.substr(0, FUNCTION_MARK_SEPARATOR) && (!obj.____baseFunctionName || !obj.____currentFunctionName)) continue; if (typeof (obj) != "function") { if (!this ) this = obj; continue; } else { if (!this ) { include =false; var baseName = obj.___baseFunctionName || n; var name = n; if(include[baseName]===undefined) { include[baseName] = this[baseName]; } if(include[baseName]) { name = FUNCTION_MARK_SEPARATOR + n; obj.___currentFunctionName = FUNCTION_MARK_SEPARATOR + obj.___currentFunctionName }else{ name = n; } this[name] = obj; } else { include = true; var baseName = obj.___baseFunctionName || n; var name = FUNCTION_MARK_SEPARATOR + n; var func = obj.bindFunction(null); func.___baseFunctionName = baseName; func.___currentFunctionName = name; this[name] = func; } if (n.substr(0, FUNCTION_MARK_SEPARATOR.length) != FUNCTION_MARK_SEPARATOR) { superResetList.push(n); } } } for (var i = 0; i < superResetList.length; i++) { var n = superResetList[i]; this.super = baseFunctionExcenter.bindFunction(this,n); } } }
还算是比较简单吧,下面看看怎么用:
function ClassA() { this.name = 'ClassA'; } ClassA.prototype = { log : function () { console.log("ClassA.log:" + this.name); } } function ClassB() { initClass(this); this.super(ClassA); this.name = 'ClassB'; } ClassB.prototype = { log : function () { console.log("ClassB.log:" + this.name); this.super.log(); } } function ClassC() { initClass(this); this.super(ClassB); this.name = 'ClassC'; } ClassC.prototype = { log : function () { console.log("ClassC.log:" + this.name); this.super.log(); } } function ClassD() { initClass(this); this.super(ClassC); this.name = 'ClassD'; } var classD = new ClassD(); console.log("测试一:") classD.log(); function ClassE(name, value) { this.name = name; this.value = value; } ClassE.prototype = { print : function () { console.log("classE.print:" + this.name + "/" + this.value); } } function ClassF(name, value) { initClass(this); this.super(ClassE, name, value); this.super(ClassD); } var classF = new ClassF("张三", "100"); console.log("测试二:") classF.log(); classF.print();
输出结果:
测试一: ClassC.log:ClassD ClassB.log:ClassD ClassA.log:ClassD 测试二: ClassC.log:张三 ClassB.log:张三 ClassA.log:张三 classE.print:张三/100
需要注意的有:
“__”:被当成私有对象处理,不会被继承
“___baseFunctionName”、“____currentFunctionName”被当成继承者信息存储属性,不能当其它使用了。
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- 最后一次说说闭包
- Ajax
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因
- Canvas 在高清屏下绘制图片变模糊的解决方法
- Redux系列02:一个炒鸡简单的react+redux例子
- JavaScript 各种遍历方式详解