admin管理员组文章数量:1391975
I have set up a base class as standard:
MyBase = function() {
this.m_Stuff = 0; // etc
};
MyBase.prototype.MySuperFunction = function (arg1) {
alert("Hello" + arg1);
};
Next I set up another class that inherits MyBase
MyChild = function () {
MyBase.call(this);
this.m_OtherStuff = 1; // etc
};
MyChild.prototype = new MyBase(); // innherit
But then (and this is the bit I dont know how to do) I want to override MyBase's MySuperFunction with a better one, but calling the base class function in the process:
MyChild.prototype.MySuperFunction = function (arg1, arg2) {
MyBase.MySuperFunction(arg1); // THIS LINE IS THE LINE I DONT KNOW HOW TO DO
alert("You is a " + arg2 + "'th level idiot");
};
Its a child class that wants to override is base class function, but wants to call the base class function in the new improved definition.
Is this possible, and if so, how can it be done?
I have set up a base class as standard:
MyBase = function() {
this.m_Stuff = 0; // etc
};
MyBase.prototype.MySuperFunction = function (arg1) {
alert("Hello" + arg1);
};
Next I set up another class that inherits MyBase
MyChild = function () {
MyBase.call(this);
this.m_OtherStuff = 1; // etc
};
MyChild.prototype = new MyBase(); // innherit
But then (and this is the bit I dont know how to do) I want to override MyBase's MySuperFunction with a better one, but calling the base class function in the process:
MyChild.prototype.MySuperFunction = function (arg1, arg2) {
MyBase.MySuperFunction(arg1); // THIS LINE IS THE LINE I DONT KNOW HOW TO DO
alert("You is a " + arg2 + "'th level idiot");
};
Its a child class that wants to override is base class function, but wants to call the base class function in the new improved definition.
Is this possible, and if so, how can it be done?
Share Improve this question asked Jun 6, 2013 at 13:14 RewindRewind 2,8245 gold badges36 silver badges64 bronze badges 2- just to clarify, so are you saying MyBase.prototype.MySuperFunction needs to alert "You is a " + arg2 + "'th level idiot" or you are just trying to invoke it in the context of the child class method? – chrisvillanueva Commented Jun 6, 2013 at 13:23
- I want my child class to call the base class' function before adding it's own amendment. – Rewind Commented Jun 6, 2013 at 14:03
2 Answers
Reset to default 8It's similar to the call in your inherited constructor. You can access the "super" method still on MyBase.prototype.MySuperFunction
(where you assigned it), so use:
MyBase.prototype.MySuperFunction.call(this, arg1);
For a more dynamic approach you even might use Object.getPrototypeOf
to get the prototype, but watch out that it works with dynamic inheritance. And if you have many methods that need to call their parent, it can be helpful to alias MyBase.prototype
as a super
variable which is accessible to all functions on the Child prototype object (see this answer for an example)).
Please apply the following:-
MyBase.prototype.MySuperFunction.call(this, arg1);
本文标签: Javascript Class Inheritance For FunctionsStack Overflow
版权声明:本文标题:Javascript Class Inheritance For Functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744655516a2617939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论