more docs...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-04-25 02:00:40 +03:00
parent ba185abada
commit c9186d6d04

View File

@ -15,13 +15,14 @@ several advantages:
- less restrictive:
- `new` is optional
- all input components are reusable
- no artificial restrictions
Disadvantages compared to the `class` syntax:
- no _syntactic sugar_
- slightly more complicated calling of _super_ or `parent` methods
## Usage
## Basic usage
```javascript
var object = require('ig-object')
@ -31,20 +32,18 @@ Create a basic constructor...
```javascript
// NOTE: new is optional here...
var A = new object.Constructor('A', {})
var A = new object.Constructor('A')
```
In _JavaScript_ constructor `B` inherits from constructor `A` iff
`B.prototypes` is _prototype_ of `A.prototype`. So to implement inheritance
`B.prototype` is _prototype_ of `A.prototype`. So to implement inheritance
we simply need to _link_ the prototypes of two constructors via `.__proto__`,
`Object.create(..)` or other means.
```javascript
// NOTE: we could simply use A() or new A() here but that would call
// the active constructors if they are defined which might not be
// desirable at definition time...
var B = object.Constructor('B', {__proto__: A.prototype})
var C = object.Constructor('C', Object.create(B.prototype))
```
@ -87,6 +86,7 @@ var Item = object.Constructor('Item', {
__init__: function(){
// call the "super" method...
object.parent(this.__init__, this).call(this)
this.item_attr = 'instance attribute value'
},
})
@ -97,7 +97,6 @@ var Item = object.Constructor('Item', {
### Callable instances
```javascript
// callable instance constructor...
var Action = object.Constructor('Action',
// constructor as a function...
function(context, ...args){
@ -107,6 +106,7 @@ var Action = object.Constructor('Action',
var action = new Action()
// the instance now is a function...
action()