mirror of
https://github.com/flynx/object.js.git
synced 2025-10-29 10:30:08 +00:00
reworked constructor + docs...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
e86758330b
commit
adc7f32f42
61
README.md
61
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
|
||||
|
||||
<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
|
||||
- [object.js](#objectjs)
|
||||
|
||||
28
object.js
28
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__')
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ig-object",
|
||||
"version": "3.0.0",
|
||||
"version": "3.1.0",
|
||||
"description": "",
|
||||
"main": "object.js",
|
||||
"scripts": {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user