From adc7f32f42197f22392f0097e161c4a8e9571caa Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 6 May 2020 02:37:08 +0300 Subject: [PATCH] reworked constructor + docs... Signed-off-by: Alex A. Naanou --- README.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++-- object.js | 28 +++++++++++++----------- package.json | 2 +- 3 files changed, 76 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2555854..d363155 100755 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ # object.js -_object.js_ provides a set of tools for making and maintaining object -constructors and for managing their inheritance relations. +_object.js_ provides a meta-constructor and a set of tools and utilities +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 @@ -24,6 +26,61 @@ Disadvantages compared to the `class` syntax: - No _syntactic sugar_ - Slightly more complicated calling of `parent` (_super_) methods + + + + + +
+ +_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 + }, +}) +``` + + + +_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 + } +} +``` + +
+ ## Contents - [object.js](#objectjs) diff --git a/object.js b/object.js index 0226f63..70b33ba 100755 --- a/object.js +++ b/object.js @@ -387,6 +387,7 @@ function(context, constructor, ...args){ // // Make a constructor with prototype extending parent-constructor... // Constructor(name, parent-constructor, proto) +// Constructor(name, parent-constructor, constructor-mixin, proto) // -> constructor // // @@ -518,29 +519,32 @@ function(context, constructor, ...args){ // the other producing a semi-broken instance. // It is however possible to mix related types as we are doing for // callable instances (Function + Object -- a function is an object). +// See README.md for more info. // // XXX revise .toString(..) definition test... var Constructor = module.Constructor = // shorthand... module.C = -function Constructor(name, a, b){ - var proto = b == null ? a : b - proto = proto || {} - var constructor_mixin = b == null ? b : a - var constructor_proto +function Constructor(name, a, b, c){ + var args = [...arguments].slice(1, 4) + + // parse args... + // 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: - // Constructor(name, constructor, { .. }) - if(constructor_mixin instanceof Function){ - constructor_proto = constructor_mixin - constructor_mixin = null - proto.__proto__ === ({}).__proto__ + // Constructor(name, constructor, ..) + if(constructor_proto){ + proto.__proto__ === Object.prototype && (proto.__proto__ = constructor_proto.prototype) // handle: - // Constructor(name, { .. }) - // Constructor(name, { .. }, { .. }) + // Constructor(name, ..) } else { // handle .__extends__ a = Object.hasOwnProperty.call(proto, '__extends__') diff --git a/package.json b/package.json index af9d7d9..d4cb8d3 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-object", - "version": "3.0.0", + "version": "3.1.0", "description": "", "main": "object.js", "scripts": {