tobie (owner)

Forks

Revisions

gist: 43064 Download_button fork
public
Public Clone URL: git://gist.github.com/43064.git
Embed All Files: show embed
JavaScript #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Simpler, more robust super keyword for Prototype.
 
Given the following parent class:
 
var Person = Class.create({
initialize: function(name) {
this.name = name;
},
say: function(message) {
return this.name + ': ' + message;
}
});
 
Subclassing with Class#callSuper:
 
var Pirate = Class.create(Person, {
say: function(message) {
return this.callSuper('say', message) + ', yarr!';
}
});
 
 
... and using Class#applySuper, you can directly pass the arguments object:
 
var Pirate = Class.create(Person, {
say: function() {
return this.applySuper('say', arguments) + ', yarr!';
}
});
 
Of course this also allows calling other methods of the subclass (Whether this is a good or a bad thing is a whole other topic).
 
var Pirate = Class.create(Person, {
say: function() {
return this.applySuper('say', arguments) + ', yarr!';
},
 
yell: function(message) {
return this.callSuper('say', message.toUpperCase());
}
});
*/
 
(function() {
  var slice = Array.prototype.slice;
  
  function callSuper(methodName) {
    return this.applySuper(methodName, slice.call(arguments, 1));
  }
 
  function applySuper(methodName, args) {
    return this.constructor.superclass.prototype[methodName].apply(this, args);
  }
  
  return {
    callSuper: callSuper,
    applySuper: applySuper
  }
})();