cleanup and docs...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-04-15 00:42:20 +03:00
parent 569d1e3f21
commit 130b6c4ef7
3 changed files with 71 additions and 42 deletions

View File

@ -56,16 +56,27 @@ c instanceof B // -> true
c instanceof A // -> true c instanceof A // -> true
``` ```
### Inheritance
```javascript ```javascript
//
// Base
// ^
// |
// Item
//
var Base = object.Constructor('Base', { var Base = object.Constructor('Base', {
proto_attr: 'prototype attr value',
get prop(){ get prop(){
return 123 }, return 'propery value' },
method: function(){ method: function(){
console.log('Base.method()') }, console.log('Base.method()') },
// initializer... // initializer...
__init__: function(){ __init__: function(){
this.base_attribute = 321 this.instance_attr = 'instance'
}, },
}) })
@ -76,19 +87,24 @@ var Item = object.Constructor('Item', {
__init__: function(){ __init__: function(){
// call the "super" method... // call the "super" method...
object.parent(this.__init__, this).call(this) object.parent(this.__init__, this).call(this)
this.item_attribute = 333 this.item_attr = 'instance attribute value'
}, },
}) })
``` ```
### Callable instances
```javascript ```javascript
// callable instance constructor... // callable instance constructor...
var Action = object.Constructor('Action', 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: // normal this, but here we have two contexts:
// - external -- where the instance was called from // - internal (this) -- the instance (this)
// - internal -- the instance (this) // - external (context) -- call context
//
// NOTE: if the prototype is explicitly defined as a function then // NOTE: if the prototype is explicitly defined as a function then
// it is the user's responsibility to call .__call__(..) method // it is the user's responsibility to call .__call__(..) method
// (see below) // (see below)
@ -104,9 +120,14 @@ action()
// a different way to do the above... // a different way to do the above...
var Action2 = object.Constructor('Action2', { 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 // 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 // NOTE: this is not called if a user defines the prototype as a function
// (see above) // (see above)
__call__: function(context, ...args){ __call__: function(context, ...args){
@ -116,13 +137,16 @@ var Action2 = object.Constructor('Action2', {
``` ```
### Low level constructor
```javascript ```javascript
// low level constructor...
var LowLevel = object.Constructor('LowLevel', { var LowLevel = object.Constructor('LowLevel', {
// Low level instance constructor... // 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: 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 // NOTE: this has priority over the callable protocols above, thus
// the user must take care of both the prototype as function and // the user must take care of both the prototype as function and
// prototype.__call__(..)... // prototype.__call__(..)...
@ -135,6 +159,12 @@ var LowLevel = object.Constructor('LowLevel', {
## Components ## Components
```
sources(<object>, <name>)
sources(<object>, <name>, <callback>)
-> <list>
```
``` ```
parent(<method>, <this>) parent(<method>, <this>)
parent(<method>, <name>, <this>) parent(<method>, <name>, <this>)

View File

@ -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. // Find the next parent method in the prototype chain.
// //
// parent(meth, this) // parent(meth, this)
@ -63,32 +90,6 @@ function(method, name, that){
return that.__proto__[name] } 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){ function Constructor(name, a, b){
var proto = b == null ? a : b var proto = b == null ? a : b
var cls_proto = b == null ? b : a var cls_proto = b == null ? b : a
proto = proto || {}
// mirror doc from target to func... // mirror doc from target to func...
var _mirror = function(func, target){ var _mirror = function(func, target){
@ -259,10 +261,6 @@ function Constructor(name, a, b){
}) })
return func } return func }
var __new__ = function(base, ...args){
}
var _constructor = function Constructor(){ var _constructor = function Constructor(){
// NOTE: the following does the job of the 'new' operator but // NOTE: the following does the job of the 'new' operator but
// with one advantage, we can now pass arbitrary args // with one advantage, we can now pass arbitrary args
@ -271,6 +269,7 @@ function Constructor(name, a, b){
// return new _constructor(json) // return new _constructor(json)
var obj = var obj =
// prototype defines .__new__(..)... // prototype defines .__new__(..)...
// XXX should the context here he _constructor or .prototype (now)???
_constructor.prototype.__new__ instanceof Function ? _constructor.prototype.__new__ instanceof Function ?
_constructor.prototype.__new__(this, ...arguments) _constructor.prototype.__new__(this, ...arguments)
// prototype is a function... // prototype is a function...

View File

@ -1,6 +1,6 @@
{ {
"name": "ig-object", "name": "ig-object",
"version": "2.1.1", "version": "2.2.0",
"description": "", "description": "",
"main": "object.js", "main": "object.js",
"scripts": { "scripts": {