Skip to content
Matthew Browne edited this page Nov 29, 2012 · 4 revisions

extend(src1 [, src2, src3, ...])

Extends a parent object (src1) with additional properties and/or methods, returning a new object with src1 as its prototype.

src1: The first source object; will be the prototype of the object returned Remaining arguments (optional): Additional source objects whose properties will be added to the object returned. Return value: A new object with src1 as its prototype and with the additional source objects (if provided) mixed in.

If any of the properties have the same name, source arguments further to the right override previous arguments.

extend() never calls the constructor of src1, making it possible to extend objects whose constructor functions have required parameters, and making it more likely to detect the potential mistake of forgetting to call the parent constructor from a child constructor (see readme).

In ECMAScript 5 environments, this method will define properties using the Object.defineProperty method and preserve all property attributes.

mixin(dst, src1 [, src2, src3, ...])

Adds ("mixes") the properties from the source objects into the destination object. If any of the properties have the same name, source arguments further to the right override previous arguments.

dst: Destination object Remaining arguments: Source objects Return value: dst

Note that unlike extend(), mixin() modifies the destination object directly, rather than returning a new object. Also unlike extend(), none of the arguments are set as the prototype of the destination object; the prototype of the destination object will be unchanged after calling mixin().

In ECMAScript 5 environments, this method will define properties using the Object.defineProperty method and preserve all property attributes.

makePrototype(ctor, def)

ctor: Constructor function def: Prototype definition Return value: def, after setting def.constructor = ctor

Primary advantage: Having the constructor property set appropriately makes debugging in the console easier. Using makePrototype() to do so promotes consistency and limits potential confusion (see example 2 in the readme).

This method is recommended to be used alone (when initially creating objects with no parent), or in conjunction with extend() or mixin() (to obtain the "def" parameter) when you want to add additional properties/methods to an existing object.

For further explanation, see example 2 in the readme.

deepCopy(obj)

Returns a deep copy (clone) of obj (makes copies of all its properties, sub-properties, etc.)

Useful for cases where the copied object should not contain any of the same memory references as the original object, e.g. a "defaults" object for user settings that can be copied and modified without ever affecting the original "defaults" object, even if the "defaults" object contains objects or arrays.

See this example.

Clone this wiki locally