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 63 64 65 66
|
Object.prototype.create = function(){ var object = clone(this); if (object.construct != undefined) object.construct.apply(object, arguments); return object; }
Object.prototype.extend = function(properties){ var result = clone(this); forEachIn(properties, function(name, value){ result[name] = value; }); return result; }
var Item = { construct: function(name){ this.name = name; }, inspect: function(){ alert("It is " + this.name + "."); }, kick: function(){ alert("Klunk!"); }, take: function(){ alert("You cannot lift " + this.name + "."); } }
var lantern = Item.create("the brass lantern");
var DetailedItem = Item.extend({ construct: function(name, details){ Item.construct.call(this, name); this.details = details; }, inspect: function(){ alert("you see " + this.name + "," + this.details + "."); } });
var giantSloth = DetailedItem.create("the giant sloth", "it is quietly hanging from a tree, munching leaves"); var SmallItem = Item.extend({ kick: function(){ alert(this.name + " files across the room."); }, take: function(){ alert("you take " + this.name + "."); } });
var pencil = SmallItem.create("the red pencil"); pencil.take();
|