Paste #88
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 | <script type="text/javascript" src="prototype.js"></script> <script type="text/javascript"> var Mammal = Class.create(); Mammal.prototype = { initialize : function(name) { this.name = name; this.offspring = []; this.type = "Mammal"; }, toString : function() { return "[" + this.type + ": " + this.name + "]"; }, haveABaby : function(name) { if (name == undefined) { name = "Baby " + this.name; } var newBaby = new this.constructor(name); this.offspring.push(newBaby); return newBaby; } }; var Human = Class.create(); Human.prototype.constructor = Human; Human.prototype = Object.extend(new Mammal, { initialize : function(name) { this.name = name; this.type = "Human"; }, }); var hoge = new Human("Hoge"); var fuga = hoge.haveABaby("Fuga"); var takashi = new Human("Takashi"); var kay = takashi.haveABaby("Kay"); alert(takashi.offspring[0].toString()); </script> |