mirror of
https://github.com/flynx/object.js.git
synced 2025-10-29 10:30:08 +00:00
added simple construction of callable instances...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
5113c0af7c
commit
499a3e0d8a
54
README.md
54
README.md
@ -82,6 +82,56 @@ var Item = object.Constructor('Item', {
|
||||
|
||||
```
|
||||
|
||||
```javascript
|
||||
// callable instance constructor...
|
||||
var Action = object.Constructor('Action',
|
||||
// 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)
|
||||
// NOTE: if the prototype is explicitly defined as a function then
|
||||
// it is the user's responsibility to call .__call__(..) method
|
||||
// (see below)
|
||||
function(context, ...args){
|
||||
// return the instance...
|
||||
return this
|
||||
})
|
||||
|
||||
var action = new Action()
|
||||
|
||||
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
|
||||
// not need to use Object.assign(..) or object.mixinFlat(..) to define
|
||||
// attributes and props...
|
||||
// NOTE: this is not called if a user defines the prototype as a function
|
||||
// (see above)
|
||||
__call__: function(context, ...args){
|
||||
return this
|
||||
},
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
```javascript
|
||||
// low level constructor...
|
||||
var LowLevel = object.Constructor('LowLevel', {
|
||||
// Low level instance constructor...
|
||||
// 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__(..)...
|
||||
__new__: function(context, ...args){
|
||||
return {}
|
||||
},
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
## Components
|
||||
|
||||
@ -102,8 +152,8 @@ mixinFlat(<root>, <object>, ...)
|
||||
```
|
||||
|
||||
```
|
||||
makeConstructor(<name>, <prototype>)
|
||||
makeConstructor(<name>, <class-prototype>, <prototype>)
|
||||
Constructor(<name>, <prototype>)
|
||||
Constructor(<name>, <class-prototype>, <prototype>)
|
||||
-> <constructor>
|
||||
```
|
||||
|
||||
|
||||
37
object.js
37
object.js
@ -219,6 +219,7 @@ function(root, ...objects){
|
||||
// ...mainly for inheritance.
|
||||
// ...would also be helpful in this case to call all the
|
||||
// constructors in the chain
|
||||
// XXX need a simple way to make a function constructor...
|
||||
var Constructor =
|
||||
module.Constructor =
|
||||
// shorthand...
|
||||
@ -227,14 +228,44 @@ function Constructor(name, a, b){
|
||||
var proto = b == null ? a : b
|
||||
var cls_proto = b == null ? b : a
|
||||
|
||||
// mirror doc from target to func...
|
||||
var _mirror = function(func, target){
|
||||
Object.defineProperty(func, 'toString', {
|
||||
value: function(...args){
|
||||
return target.toString(...args) },
|
||||
enumerable: false,
|
||||
})
|
||||
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
|
||||
// in...
|
||||
// This is equivalent to:
|
||||
// return new _constructor(json)
|
||||
var obj = _constructor.prototype.__new__ instanceof Function ?
|
||||
_constructor.prototype.__new__({}, ...arguments)
|
||||
var obj =
|
||||
// prototype defines .__new__(..)...
|
||||
_constructor.prototype.__new__ instanceof Function ?
|
||||
_constructor.prototype.__new__(this, ...arguments)
|
||||
// prototype is a function...
|
||||
// NOTE: we need to isolate the .prototype from instances...
|
||||
: _constructor.prototype instanceof Function ?
|
||||
_mirror(
|
||||
function(){
|
||||
return _constructor.prototype.call(obj, this, ...arguments) },
|
||||
_constructor.prototype)
|
||||
// prototype defines .__call__(..)...
|
||||
// NOTE: we need to isolate the .__call__ from instances...
|
||||
: _constructor.prototype.__call__ instanceof Function ?
|
||||
_mirror(
|
||||
function(){
|
||||
return _constructor.prototype.__call__.call(obj, this, ...arguments) },
|
||||
_constructor.prototype.__call__)
|
||||
// default object base...
|
||||
: {}
|
||||
|
||||
obj.__proto__ = _constructor.prototype
|
||||
@ -257,7 +288,7 @@ function Constructor(name, a, b){
|
||||
.toString()
|
||||
.replace(/Constructor/g, name))
|
||||
|
||||
// set an informative .toString...
|
||||
// set an informative Constructor .toString(..)...
|
||||
// NOTE: do this only if .toString(..) is not defined by user...
|
||||
;((cls_proto || {}).toString() == ({}).toString())
|
||||
// XXX is this the right way to go or should we set this openly???
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ig-object",
|
||||
"version": "2.0.0",
|
||||
"version": "2.1.0",
|
||||
"description": "",
|
||||
"main": "object.js",
|
||||
"scripts": {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user