/**
* 继承方法
* @param {Object} _this [要继承的方法所处对象,传空则默认为window]
* @param {String} thisName [要继承方法的名称]
* @param {Function} child [子方法]
*/
function extend(_this, thisName, child){
_this = _this || window;
var base = _this[thisName];
_this[thisName] = function(){
return extend.callFun(base, _this, child);
}
}
/**
* 调用子方法
* @param {Function} base [父方法]
* @param {Object} scope [作用域 方法所处对象]
* @param {Function} child [子方法]
* @return {} [child方法返回什么就返回什么]
*/
extend.callFun = function(base, scope, child){
return child.apply(scope, arguments.callee.caller.arguments);
}
/**
* 获取父方法,必须在子方法中调用
* @return {Function}
*/
extend.getParent = function(){
return arguments.callee.caller.caller.arguments[0];
}
/**
* 调用父方法
* @return {} [父方法返回什么就返回什么]
*/
extend.execParent = function(){
var tempArgs = arguments.callee.caller.caller.arguments;
return tempArgs[0].apply(tempArgs[1], arguments.callee.caller.arguments);
}
var obj = {
a: function(text){
console.log("不存在的操作");
}
}
function b(text){
switch(text){
case "1":
console.log("1号操作");
break;
default:
extend.execParent();
}
}
function c(text){
switch(text){
case "2":
console.log("2号操作");
break;
default:
extend.execParent();
}
}
// 继承a和b
extend(obj, "a", b);
extend(obj, "a", c);
obj.a("1"); // 1号操作
obj.a("2"); // 2号操作
obj.a("3"); // 不存在的操作
项目中有一个方法里写大量的case,想分割开,就写了这个