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
// athlete.js
function Athlete(name) {
  this.attack = function(opponent) {
    print(name + ' attacked ' + opponent.name);
  };
}

// footballer.js
function Footballer(name, position) {
  var self = new Athlete(name);

  self.kick = function() {
    print(this.name + ' shoots...');

    shoot({
      onSuccess: function() { print(name + ' scores!'); }, 
      onFailure: function() { print('Juuust a bit outside.'); } 
    });

  return self;

  function shoot(callbacks) {
    // ...
  }
};

// defender.js
function Defender(name) {
  var self = new Footballer(name, 'defender');

  var superAttack = function() { return self.attack; }();
  self.attack = function() {
    superAttack();

    slideTackle();
    feignInjury();
  };

  return self;

  function slideTackle() {
    // ...
  };

  function feignInjury() {
    // ...
  };
}