mirror of
https://github.com/flynx/object.js.git
synced 2025-10-30 19:10:11 +00:00
more docs...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
ba185abada
commit
c9186d6d04
22
README.md
22
README.md
@ -15,13 +15,14 @@ several advantages:
|
|||||||
- less restrictive:
|
- less restrictive:
|
||||||
- `new` is optional
|
- `new` is optional
|
||||||
- all input components are reusable
|
- all input components are reusable
|
||||||
|
- no artificial restrictions
|
||||||
|
|
||||||
Disadvantages compared to the `class` syntax:
|
Disadvantages compared to the `class` syntax:
|
||||||
- no _syntactic sugar_
|
- no _syntactic sugar_
|
||||||
- slightly more complicated calling of _super_ or `parent` methods
|
- slightly more complicated calling of _super_ or `parent` methods
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Basic usage
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var object = require('ig-object')
|
var object = require('ig-object')
|
||||||
@ -31,20 +32,18 @@ Create a basic constructor...
|
|||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// NOTE: new is optional here...
|
// 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
|
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__`,
|
we simply need to _link_ the prototypes of two constructors via `.__proto__`,
|
||||||
`Object.create(..)` or other means.
|
`Object.create(..)` or other means.
|
||||||
|
|
||||||
```javascript
|
```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 B = object.Constructor('B', {__proto__: A.prototype})
|
||||||
|
|
||||||
var C = object.Constructor('C', Object.create(B.prototype))
|
var C = object.Constructor('C', Object.create(B.prototype))
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -87,6 +86,7 @@ var Item = object.Constructor('Item', {
|
|||||||
__init__: function(){
|
__init__: function(){
|
||||||
// call the "super" method...
|
// call the "super" method...
|
||||||
object.parent(this.__init__, this).call(this)
|
object.parent(this.__init__, this).call(this)
|
||||||
|
|
||||||
this.item_attr = 'instance attribute value'
|
this.item_attr = 'instance attribute value'
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@ -97,7 +97,6 @@ var Item = object.Constructor('Item', {
|
|||||||
### Callable instances
|
### Callable instances
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// callable instance constructor...
|
|
||||||
var Action = object.Constructor('Action',
|
var Action = object.Constructor('Action',
|
||||||
// constructor as a function...
|
// constructor as a function...
|
||||||
function(context, ...args){
|
function(context, ...args){
|
||||||
@ -107,6 +106,7 @@ var Action = object.Constructor('Action',
|
|||||||
|
|
||||||
var action = new Action()
|
var action = new Action()
|
||||||
|
|
||||||
|
// the instance now is a function...
|
||||||
action()
|
action()
|
||||||
|
|
||||||
|
|
||||||
@ -127,9 +127,9 @@ var Action2 = object.Constructor('Action2', {
|
|||||||
In the above cases both the base function and the `.__call__(..)` method
|
In the above cases both the base function and the `.__call__(..)` method
|
||||||
receive a `context` argument in addition to `this` context, those represent
|
receive a `context` argument in addition to `this` context, those represent
|
||||||
the two contexts relevant to the callable instance:
|
the two contexts relevant to the callable instance:
|
||||||
- Internal context (`this`)
|
- Internal context (`this`)
|
||||||
This always references the instance being called
|
This always references the instance being called
|
||||||
- External context (`context`)
|
- External context (`context`)
|
||||||
This is the object the instance is called from (`window` or `global` by
|
This is the object the instance is called from (`window` or `global` by
|
||||||
default), i.e. _the thing before the dot_
|
default), i.e. _the thing before the dot_
|
||||||
|
|
||||||
@ -155,9 +155,9 @@ references the `.prototype` object.
|
|||||||
The external context is the same as above.
|
The external context is the same as above.
|
||||||
|
|
||||||
Contexts:
|
Contexts:
|
||||||
- Internal context (`this`)
|
- Internal context (`this`)
|
||||||
References the `.prototype` of the constructor.
|
References the `.prototype` of the constructor.
|
||||||
- External context (`context`)
|
- External context (`context`)
|
||||||
This is the object the instance is called from (`window` or `global` by
|
This is the object the instance is called from (`window` or `global` by
|
||||||
default), i.e. _the thing before the dot_, the same as for function
|
default), i.e. _the thing before the dot_, the same as for function
|
||||||
constructor and `.__call__(..)`.
|
constructor and `.__call__(..)`.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user