Assume I have a class like this:
JavaScript
x
10
10
1
function Widget() {
2
this.id = new Date().getTime();
3
// other fields
4
}
5
Widget.prototype = {
6
load: function(args) {
7
// do something
8
}
9
}
10
From this class I created some other classes which inherit the same prototype but have some added methods. What I want to do is being able to define a load() method in the sub-classes which first calls the parent method and then execute some code. Something like:
JavaScript
1
7
1
SpecialWidget.prototype = {
2
load: function(args) {
3
super.load(args);
4
// specific code here
5
}
6
}
7
I know there’s no super keyword in Javascript but there must be a way to do this.
Advertisement
Answer
You can simulate it like this:
JavaScript
1
7
1
SpecialWidget.prototype = {
2
load: function(args) {
3
Widget.prototype.load.call(this, args);
4
// specific code here
5
}
6
}
7
Or you can create your own super property like this:
JavaScript
1
9
1
SpecialWidget.prototype.parent = Widget.prototype;
2
3
SpecialWidget.prototype = {
4
load: function(args) {
5
this.parent.load.call(this,args);
6
// specific code here
7
}
8
}
9