Paste #89

 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
<script type="text/javascript">

(function() {
  window.Hoge = {};
})();

Object.prototype.Inherits = function( parent )
{
  if( arguments.length > 1 )
  {
    parent.apply( this, Array.prototype.slice.call( arguments, 1 ) );
  }
  else
  {
    parent.call( this );
  }
}

Function.prototype.Inherits = function( parent )
{
  this.prototype = new parent();
  this.prototype.constructor = this;
}


Hoge.Mammal = function(name) {
  this.name = name;
  this.race = "Mammal";
  this.offspring = [];
}

Hoge.Mammal.prototype.haveABaby = function(name) {
  if (name === undefined) {
    name = "Baby " + this.name;
  }
  var newBaby = new this.constructor(name);
  this.offspring.push(newBaby);
  return newBaby;
}

Hoge.Mammal.prototype.toString = function() {
  return "[" + this.race + ": " + this.name + "]";
};

Hoge.Human = function(name) {
  this.Inherits(Hoge.Mammal, name);
  this.race = "Human";
}
Hoge.Human.Inherits(Hoge.Mammal);

var takashi = new Hoge.Human("Takashi");
var age = new Hoge.Human("Age");
age.haveABaby("Sora");
age.haveABaby("Nene");
var kay = takashi.haveABaby("Kay");
alert(takashi.offspring[0].toString());

</script>