Last active
March 1, 2018 03:04
-
-
Save Gaubee/5013908 to your computer and use it in GitHub Desktop.
javascript继承复写原型链函数的this._super()实现调用原函数(ES3版)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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