JavaScript
// Borrow methods from one class for use by another. // The arguments should be the constructor functions for the classes // Methods of built-in types such as Object, Array, Date and RegExp are // not enumerable and cannot be borrowed with this method. function borrowMethods(borrowFrom, addTo) { var from = borrowFrom.prototype; // prototype object to borrow from var to = addTo.prototype; // prototype object to extend for(m in from) { // Loop through all properties of the prototye if (typeof from[m] != "function") continue; // ignore nonfunctions to[m] = from[m]; // borrow the method } }
// This class isn't good for much on its own. But it does define a // generic toString() method that may be of interest to other classes. function GenericToString() {} GenericToString.prototype.toString = function() { var props = []; for(var name in this) { if (!this.hasOwnProperty(name)) continue; var value = this[name]; var s = name + ":" switch(typeof value) { case 'function': s += "function"; break; case 'object': if (value instanceof Array) s += "array" else s += value.toString(); break; default: s += String(value); break; } props.push(s); } return "{" + props.join(", ") + "}"; } // This mixin class defines an equals() method that can compare // simple objects for equality. function GenericEquals() {} GenericEquals.prototype.equals = function(that) { if (this == that) return true; // this and that are equal only if this has all the properties of // that and doesn't have any additional properties // Note that we don't do deep comparison. Property values // must be === to each other. So properties that refer to objects // must refer to the same object, not objects that are equals() var propsInThat = 0; for(var name in that) { propsInThat++; if (this[name] !== that[name]) return false; } // Now make sure that this object doesn't have additional props var propsInThis = 0; for(name in this) propsInThis++; // If this has additional properties then they are not equal if (propsInThis != propsInThat) return false; // The two objects appear to be equal. return true; } borrowMethods(GenericEquals, Rectangle); borrowMethods(GenericToString, Rectangle)
59 of 65