diff --git a/README.md b/README.md index 9abe502..6cf1961 100755 --- a/README.md +++ b/README.md @@ -56,16 +56,27 @@ c instanceof B // -> true c instanceof A // -> true ``` + +### Inheritance ```javascript +// +// Base +// ^ +// | +// Item +// var Base = object.Constructor('Base', { + proto_attr: 'prototype attr value', + get prop(){ - return 123 }, + return 'propery value' }, + method: function(){ console.log('Base.method()') }, // initializer... __init__: function(){ - this.base_attribute = 321 + this.instance_attr = 'instance' }, }) @@ -76,19 +87,24 @@ var Item = object.Constructor('Item', { __init__: function(){ // call the "super" method... object.parent(this.__init__, this).call(this) - this.item_attribute = 333 + this.item_attr = 'instance attribute value' }, }) ``` + +### Callable instances ```javascript // callable instance constructor... var Action = object.Constructor('Action', - // the first argument is allways the external call context, like + // Define a constructor as a function... + // + // The first argument is allways the external call context, like // normal this, but here we have two contexts: - // - external -- where the instance was called from - // - internal -- the instance (this) + // - internal (this) -- the instance (this) + // - external (context) -- call context + // // NOTE: if the prototype is explicitly defined as a function then // it is the user's responsibility to call .__call__(..) method // (see below) @@ -104,9 +120,14 @@ action() // a different way to do the above... var Action2 = object.Constructor('Action2', { - // this is the same as the above but a bit more convenient as we do + // This is the same as the above but a bit more convenient as we do // not need to use Object.assign(..) or object.mixinFlat(..) to define - // attributes and props... + // attributes and props. + // + // Contexts: + // - internal (this) -- the instance + // - external (context) -- call context + // // NOTE: this is not called if a user defines the prototype as a function // (see above) __call__: function(context, ...args){ @@ -116,13 +137,16 @@ var Action2 = object.Constructor('Action2', { ``` +### Low level constructor ```javascript -// low level constructor... var LowLevel = object.Constructor('LowLevel', { // Low level instance constructor... + // + // Contexts: + // - internal (this) -- .prototype + // - external (context) -- call context + // // NOTE: if this is defined the return value is used as the instance - // NOTE: this is run in the context of the .prototype rather than - // the instance... // NOTE: this has priority over the callable protocols above, thus // the user must take care of both the prototype as function and // prototype.__call__(..)... @@ -135,6 +159,12 @@ var LowLevel = object.Constructor('LowLevel', { ## Components +``` +sources(, ) +sources(, , ) + -> +``` + ``` parent(, ) parent(, , ) diff --git a/object.js b/object.js index 05d0b78..7d537e7 100755 --- a/object.js +++ b/object.js @@ -37,6 +37,33 @@ function(text){ //--------------------------------------------------------------------- +// Get a list of sources/definitions for a prop/attr... +// +// sources(obj, name) +// sources(obj, name, callback) +// -> list +// +// +// XXX revise name... +var sources = +module.sources = +function(that, name, callback){ + var stop + var res = [] + do { + if(that.hasOwnProperty(name)){ + res.push(that) + // handle callback... + stop = callback + && callback(that) + // stop requested by callback... + if(stop === false || stop == 'stop'){ + return that } } + that = that.__proto__ + } while(that !== null) + return res } + + // Find the next parent method in the prototype chain. // // parent(meth, this) @@ -63,32 +90,6 @@ function(method, name, that){ return that.__proto__[name] } -// Get a list of prototypes that have a prop/attr defined ... -// -// defines(obj, name) -// defines(obj, name, callback) -// -> list -// -// XXX revise name... -var defines = -module.defines = -function(that, name, callback){ - var stop - var res = [] - do { - if(that.hasOwnProperty(name)){ - res.push(that) - // handle callback... - stop = callback - && callback(that) - // stop requested by callback... - if(stop === false || stop == 'stop'){ - return that } } - that = that.__proto__ - } while(that !== null) - return res } - - //--------------------------------------------------------------------- @@ -249,6 +250,7 @@ module.C = function Constructor(name, a, b){ var proto = b == null ? a : b var cls_proto = b == null ? b : a + proto = proto || {} // mirror doc from target to func... var _mirror = function(func, target){ @@ -259,10 +261,6 @@ function Constructor(name, a, b){ }) return func } - var __new__ = function(base, ...args){ - - } - var _constructor = function Constructor(){ // NOTE: the following does the job of the 'new' operator but // with one advantage, we can now pass arbitrary args @@ -271,6 +269,7 @@ function Constructor(name, a, b){ // return new _constructor(json) var obj = // prototype defines .__new__(..)... + // XXX should the context here he _constructor or .prototype (now)??? _constructor.prototype.__new__ instanceof Function ? _constructor.prototype.__new__(this, ...arguments) // prototype is a function... diff --git a/package.json b/package.json index 64f073a..f07293e 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-object", - "version": "2.1.1", + "version": "2.2.0", "description": "", "main": "object.js", "scripts": {