diff --git a/object.js b/object.js index f15268d..a49d154 100755 --- a/object.js +++ b/object.js @@ -35,6 +35,63 @@ function(text){ } + +//--------------------------------------------------------------------- + +// Find the next parent method in the prototype chain. +// +// parent(meth, this) +// parent(meth, name, this) +// -> meth +// +// +// NOTE: there are cases where method.name is not set, so a name can be +// passed explicitly. +// NOTE: this is super(..) replacement... +// NOTE: if method is root (no super method) this will return undefined. +// +// XXX should this return the method or the object??? +var parent = +module.parent = +function(method, name, that){ + if(arguments.length == 2){ + that = name + name = method.name + } + // find the current method in the prototype chain... + while(!that.hasOwnProperty(name) || that[name] !== method){ + that = that.__proto__ + } + // return the next method... + return that.__proto__[name] +} + + + +//--------------------------------------------------------------------- + +// Create an object and mix in sets of methods... +// +// mixin(root, object, ...) +// -> object +// +// This will create a new object per set of methods given and +// Object.assign(..) the method set into this object leaving the +// original objects intact. +// +// root <-- object1_copy <-- .. <-- objectN_copy +// +// +var mixin = +module.mixin = +function(root, ...objects){ + return objects + .reduce(function(res, cur){ + return Object.assign(Object.create(res), cur) }, root) } + + + + //--------------------------------------------------------------------- // Make a JavaScrip object constructor... // @@ -241,58 +298,6 @@ function makeConstructor(name, a, b){ } -// super equivalent... -// -// superMethod(, ).call(this, ...) -// -> -// -// This will return a next method in inheritance chain after by -// its name (). -// In the normal use-case is the current class and -// is the name of the current method. -var superMethod = -module.superMethod = -function superMethod(cls, meth){ - return cls.prototype.__proto__[meth] -} - - -//--------------------------------------------------------------------- - -// super replacement... -// -// parent(meth, this) -// parent(meth, name, this) -// -> meth -// -// -// XXX move this to object.js -var parent = -module.parent = -function(meth, name, that){ - if(arguments.length == 2){ - that = name - name = meth.name - } - while(!that.hasOwnProperty(name) || that[name] !== meth){ - that = that.__proto__ - } - return that.__proto__[name] -} - - - -//--------------------------------------------------------------------- - -// XXX move this to object.js -var mixin = -module.mixin = -function(root, ...objects){ - return objects - .reduce(function(res, cur){ - return Object.assign(Object.create(res), cur) }, root) } - - /********************************************************************** diff --git a/package.json b/package.json index 1acfe27..8d33411 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-object", - "version": "1.1.0", + "version": "1.2.0", "description": "", "main": "object.js", "scripts": {