Skip to content

Instantly share code, notes, and snippets.

@Gaubee
Last active March 1, 2018 03:04
Show Gist options
  • Save Gaubee/5013908 to your computer and use it in GitHub Desktop.
Save Gaubee/5013908 to your computer and use it in GitHub Desktop.
javascript继承复写原型链函数的this._super()实现调用原函数(ES3版)
log = console.log;
var c = function(){};
c.create = function( Config ){
var newFn = function(){
//return newFn;
};
newFn.prototype = new c;
var fn = c.prototype;
for (var i in Config)
{
if ((typeof fn[i]) === "function"){//if repeat
(function(item){
var oldFn = Config[item];
newFn.prototype[item] = function(){
this._super = fn[item];
oldFn.apply(this,arguments);
//this._super = Config["_super"];
delete this._super;
}
})(i);
}else{
newFn.prototype[i] = Config[i];
}
};
return new newFn;
}
c.prototype = {
say:function(){
log("old say hello");
},
id:'ider',
};
var cc = c.create({
say:function(){
log("new say");
this._super();
},
_super:function(){
log("I'm super");
}
});
cc.say();
cc._super();
log(cc.id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment