mirror of
https://github.com/flynx/object.js.git
synced 2025-10-29 18:40:08 +00:00
doc tweak...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
09cb1ac785
commit
a127225f67
80
README.md
80
README.md
@ -16,8 +16,8 @@ several advantages:
|
|||||||
- Simple way to define callable instances (including a-la _Python's_
|
- Simple way to define callable instances (including a-la _Python's_
|
||||||
`.__call__(..)`)
|
`.__call__(..)`)
|
||||||
- Less restrictive:
|
- Less restrictive:
|
||||||
- `new` is optional
|
- `new` is optional
|
||||||
- all input components are reusable
|
- all input components are reusable
|
||||||
- no artificial restrictions
|
- no artificial restrictions
|
||||||
|
|
||||||
Disadvantages compared to the `class` syntax:
|
Disadvantages compared to the `class` syntax:
|
||||||
@ -76,33 +76,33 @@ c instanceof A // -> true
|
|||||||
### Inheritance
|
### Inheritance
|
||||||
```javascript
|
```javascript
|
||||||
//
|
//
|
||||||
// Base <--- Item
|
// Base <--- Item
|
||||||
//
|
//
|
||||||
var Base = object.Constructor('Base', {
|
var Base = object.Constructor('Base', {
|
||||||
proto_attr: 'prototype attr value',
|
proto_attr: 'prototype attr value',
|
||||||
|
|
||||||
get prop(){
|
get prop(){
|
||||||
return 'propery value' },
|
return 'propery value' },
|
||||||
|
|
||||||
method: function(){
|
method: function(){
|
||||||
console.log('Base.method()') },
|
console.log('Base.method()') },
|
||||||
|
|
||||||
// initializer...
|
// initializer...
|
||||||
__init__: function(){
|
__init__: function(){
|
||||||
this.instance_attr = 'instance'
|
this.instance_attr = 'instance'
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
var Item = object.Constructor('Item', {
|
var Item = object.Constructor('Item', {
|
||||||
// inherit from Base...
|
// inherit from Base...
|
||||||
__proto__: Base.prototype,
|
__proto__: Base.prototype,
|
||||||
|
|
||||||
__init__: function(){
|
__init__: function(){
|
||||||
// call the "super" method...
|
// call the "super" method...
|
||||||
object.parentCall(this.prototype.__init__, this)
|
object.parentCall(this.prototype.__init__, this)
|
||||||
|
|
||||||
this.item_attr = 'instance attribute value'
|
this.item_attr = 'instance attribute value'
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -112,11 +112,11 @@ var Item = object.Constructor('Item', {
|
|||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var Action = object.Constructor('Action',
|
var Action = object.Constructor('Action',
|
||||||
// constructor as a function...
|
// constructor as a function...
|
||||||
function(context, ...args){
|
function(context, ...args){
|
||||||
// return the instance...
|
// return the instance...
|
||||||
return this
|
return this
|
||||||
})
|
})
|
||||||
|
|
||||||
var action = new Action()
|
var action = new Action()
|
||||||
|
|
||||||
@ -131,9 +131,9 @@ action()
|
|||||||
// attributes and props.
|
// attributes and props.
|
||||||
|
|
||||||
var Action2 = object.Constructor('Action2', {
|
var Action2 = object.Constructor('Action2', {
|
||||||
__call__: function(context, ...args){
|
__call__: function(context, ...args){
|
||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -143,7 +143,7 @@ method receive a `context` argument in addition to `this` context, those
|
|||||||
represent the two contexts relevant to the callable instance:
|
represent 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, i.e. the call _context_
|
This is the object the instance is called from, i.e. the call _context_
|
||||||
(`window` or `global` by default)
|
(`window` or `global` by default)
|
||||||
|
|
||||||
@ -155,9 +155,9 @@ user's responsibility to call `.__call__(..)` method.
|
|||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var LowLevel = object.Constructor('LowLevel', {
|
var LowLevel = object.Constructor('LowLevel', {
|
||||||
__new__: function(context, ...args){
|
__new__: function(context, ...args){
|
||||||
return {}
|
return {}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -171,7 +171,7 @@ 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, i.e. the call _context_
|
This is the object the instance is called from, i.e. the call _context_
|
||||||
(`window` or `global` by default), the same as for function constructor
|
(`window` or `global` by default), the same as for function constructor
|
||||||
and `.__call__(..)`.
|
and `.__call__(..)`.
|
||||||
@ -198,7 +198,7 @@ Get sources for attribute
|
|||||||
```
|
```
|
||||||
sources(<object>, <name>)
|
sources(<object>, <name>)
|
||||||
sources(<object>, <name>, <callback>)
|
sources(<object>, <name>, <callback>)
|
||||||
-> <list>
|
-> <list>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
@ -209,7 +209,7 @@ parent(<prototype>, <name>)
|
|||||||
-> undefined
|
-> undefined
|
||||||
|
|
||||||
parent(<method>, <this>)
|
parent(<method>, <this>)
|
||||||
-> <parent-method>
|
-> <parent-method>
|
||||||
-> undefined
|
-> undefined
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -231,11 +231,11 @@ parentProperty(<prototype>, <name>)
|
|||||||
Get parent method and call it
|
Get parent method and call it
|
||||||
```
|
```
|
||||||
parentCall(<prototype>, <name>, <this>)
|
parentCall(<prototype>, <name>, <this>)
|
||||||
-> <result>
|
-> <result>
|
||||||
-> undefined
|
-> undefined
|
||||||
|
|
||||||
parentCall(<method>, <this>)
|
parentCall(<method>, <this>)
|
||||||
-> <result>
|
-> <result>
|
||||||
-> undefined
|
-> undefined
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -243,14 +243,14 @@ parentCall(<method>, <this>)
|
|||||||
Mixin objects into a prototype chain
|
Mixin objects into a prototype chain
|
||||||
```
|
```
|
||||||
mixin(<root>, <object>, ..)
|
mixin(<root>, <object>, ..)
|
||||||
-> <object>
|
-> <object>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Mixin contents of objects into one
|
Mixin contents of objects into one
|
||||||
```
|
```
|
||||||
mixinFlat(<root>, <object>, ..)
|
mixinFlat(<root>, <object>, ..)
|
||||||
-> <object>
|
-> <object>
|
||||||
```
|
```
|
||||||
This is like `Object.assign(..)` but copies property objects rather than
|
This is like `Object.assign(..)` but copies property objects rather than
|
||||||
property values.
|
property values.
|
||||||
@ -269,14 +269,14 @@ Define an object constructor
|
|||||||
```
|
```
|
||||||
Constructor(<name>, <prototype>)
|
Constructor(<name>, <prototype>)
|
||||||
Constructor(<name>, <class-prototype>, <prototype>)
|
Constructor(<name>, <class-prototype>, <prototype>)
|
||||||
-> <constructor>
|
-> <constructor>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Shorthand to `Constructor(..)`
|
Shorthand to `Constructor(..)`
|
||||||
```
|
```
|
||||||
C(<name>, ..)
|
C(<name>, ..)
|
||||||
-> <constructor>
|
-> <constructor>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user