reworked constructor + docs...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-05-06 02:37:08 +03:00
parent e86758330b
commit adc7f32f42
3 changed files with 76 additions and 15 deletions

View File

@ -1,7 +1,9 @@
# object.js # object.js
_object.js_ provides a set of tools for making and maintaining object _object.js_ provides a meta-constructor and a set of tools and utilities
constructors and for managing their inheritance relations. to aid in object/instance construction and implementing dynamic data and
functionality inheritance within the established JavaScript prototypical
object model and interfaces.
This is an alternative to the ES6 `class` syntax in JavaScript and provides This is an alternative to the ES6 `class` syntax in JavaScript and provides
@ -24,6 +26,61 @@ Disadvantages compared to the `class` syntax:
- No _syntactic sugar_ - No _syntactic sugar_
- Slightly more complicated calling of `parent` (_super_) methods - Slightly more complicated calling of `parent` (_super_) methods
<table>
<tr valign="top">
<td width="50%">
_object.js_
```javascript
var Base = object.Constructor('X', Array, {
constructor_attr: 'constructor',
method: function(){
return 'constructor'
},
}, {
// prototype attribute...
attr: 'prototype',
get prop(){
return 42 },
__init__: function(){
this.instance_attr = 7
},
})
```
</td>
<td>
_ES6_
```javascript
class X extends Array {
static constructor_attr = 'class'
static method(){
return 'class'
}
// instance attribute with default value...
attr = 'instance'
get prop(){
return 42 }
constructor(){
super(...arguments)
this.instance_attr = 7
}
}
```
</td>
</tr>
</table>
## Contents ## Contents
- [object.js](#objectjs) - [object.js](#objectjs)

View File

@ -387,6 +387,7 @@ function(context, constructor, ...args){
// //
// Make a constructor with prototype extending parent-constructor... // Make a constructor with prototype extending parent-constructor...
// Constructor(name, parent-constructor, proto) // Constructor(name, parent-constructor, proto)
// Constructor(name, parent-constructor, constructor-mixin, proto)
// -> constructor // -> constructor
// //
// //
@ -518,29 +519,32 @@ function(context, constructor, ...args){
// the other producing a semi-broken instance. // the other producing a semi-broken instance.
// It is however possible to mix related types as we are doing for // It is however possible to mix related types as we are doing for
// callable instances (Function + Object -- a function is an object). // callable instances (Function + Object -- a function is an object).
// See README.md for more info.
// //
// XXX revise .toString(..) definition test... // XXX revise .toString(..) definition test...
var Constructor = var Constructor =
module.Constructor = module.Constructor =
// shorthand... // shorthand...
module.C = module.C =
function Constructor(name, a, b){ function Constructor(name, a, b, c){
var proto = b == null ? a : b var args = [...arguments].slice(1, 4)
proto = proto || {}
var constructor_mixin = b == null ? b : a // parse args...
var constructor_proto // Constructor(name[[, constructor[, mixin]], proto])
var proto = args.pop() || {}
var constructor_proto = args[0] instanceof Function ?
args.shift()
: undefined
var constructor_mixin = args.pop()
// handle: // handle:
// Constructor(name, constructor, { .. }) // Constructor(name, constructor, ..)
if(constructor_mixin instanceof Function){ if(constructor_proto){
constructor_proto = constructor_mixin proto.__proto__ === Object.prototype
constructor_mixin = null
proto.__proto__ === ({}).__proto__
&& (proto.__proto__ = constructor_proto.prototype) && (proto.__proto__ = constructor_proto.prototype)
// handle: // handle:
// Constructor(name, { .. }) // Constructor(name, ..)
// Constructor(name, { .. }, { .. })
} else { } else {
// handle .__extends__ // handle .__extends__
a = Object.hasOwnProperty.call(proto, '__extends__') a = Object.hasOwnProperty.call(proto, '__extends__')

View File

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