From 8d3e701d4c49b5b1f1077e7453ffde44f9427d65 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Mon, 27 Apr 2020 16:42:31 +0300 Subject: [PATCH] fixes and more experiments... Signed-off-by: Alex A. Naanou --- README.md | 2 +- object.js | 27 ++++++++++++++++++--------- package.json | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 138da66..8e309ea 100755 --- a/README.md +++ b/README.md @@ -241,7 +241,7 @@ makeRawInstance(, , ...) -> ``` -_EXPERIMENTAL: a shorthand to this is defined as `Constructor.__rawinstance__(..)`_ +_EXPERIMENTAL: a shorthand to this is defined as `Constructor.__rawinstance__(context, ..)`_ Define an object constructor ``` diff --git a/object.js b/object.js index a44f066..c99aaf4 100755 --- a/object.js +++ b/object.js @@ -138,7 +138,7 @@ function(proto, name, that){ var method = proto proto = that = name name = method.name - // skip until we get to the method... + // skip until we get to the current method... while(proto.__proto__ && proto[name] !== method){ proto = proto.__proto__ } @@ -168,6 +168,11 @@ function(proto, name, that){ // or: // parent(method, this).call(this, ...) // NOTE: for more docs see parent(..) +// +// XXX should we rename this to parent.call(..) ??? +// ...this does not care about context so there is no reason to keep +// the default call, but this lowers discoverability and might be +// confusing... var parentCall = module.parentCall = function(proto, name, that, ...args){ @@ -244,6 +249,7 @@ function(root, ...objects){ // // This will not call .__init__(..) // +// NOTE: context is only passed to .__new__(..) if defined... // NOTE: as this simply an extension to the base JavaScript protocol this // can be used to construct using any object... // Example: @@ -403,15 +409,13 @@ function Constructor(name, a, b){ var cls_proto = b == null ? b : a proto = proto || {} - // XXX EXPERIMENTAL... - var _rawinstance = function(){ - return (_constructor.__proto__ || {}).__rawinstance__ ? - _constructor.__proto__.__rawinstance__.call(this, ...arguments) - : makeRawInstance(this, _constructor, ...arguments) } - // the actual constructor... var _constructor = function Constructor(){ - var obj = _rawinstance.call(this, ...arguments) + // create raw instance... + var obj = _constructor.__rawinstance__ ? + _constructor.__rawinstance__(this, ...arguments) + : makeRawInstance(this, _constructor, ...arguments) + // initialize... obj.__init__ instanceof Function && obj.__init__(...arguments) return obj } @@ -446,7 +450,12 @@ function Constructor(name, a, b){ }) _constructor.__proto__ = cls_proto _constructor.prototype = proto - _constructor.__rawinstance__ = _rawinstance + // XXX EXPERIMENTAL... + // generic raw instance constructor... + _constructor.__rawinstance__ + || (_constructor.__rawinstance__ = + function(context, ...args){ + return makeRawInstance(context, this, ...args) }) // set .prototype.constructor Object.defineProperty(_constructor.prototype, 'constructor', { diff --git a/package.json b/package.json index e110716..0f914af 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-object", - "version": "2.4.4", + "version": "2.4.5", "description": "", "main": "object.js", "scripts": {