AbtractObject
AbstractObject with Object State Supports and free
method provides.
The derived class should overwrite the initialize
and finalize
methods.
Changes
V2.1.x
- add the state-able ability to any class.
- move RefObject to ref-object
- decoupled the abstract-object completely.
- All parts can be used individually.
- stateable = require('abstract-object/ability')
- eventable = require('events-ex/eventable')
- refCountable = require('ref-object/ability')
V2.x
- separate eventable from AbstractObject
- the new EventableObject can be as AbstractObject@v1.x
- separate eventable from RefObject too
- the new EventableRefObject can be as RefObject@v1.x
- add the eventable function to eventable any class('abstract-object/eventable').
- use the eventable plugin(events-ex) to implement eventable object.
- move all util functions to util-ex
- move enhanced inheritance Functions to inherits-ex.
- move AbstractError to abstract-error
V1.x
- AbstractObject inherits from EventEmitter.
- RefObject inherits from AbstractObject
- AbstractError
State-able Ability(V2.1.x)
add the Object State Supports and free
method to your class directly.
stateable = require 'abstract-object/ability' module.exports = stateable AbstractObject
Note: the eventable ability must be after the stateable. like this:
stateable = require 'abstract-object/ability'eventable = require 'events-ex/ability' stateable MyObject eventable MyObject # MUST BE after stateable
AbstractObject
-
Methods:
create
(class, ...): thecreate
class method uses to create a new object instance(the util.createObject is the same function).class
: the class constructor to create a new instance....
: the left arguments will be passed into the class constructor.
createWith
(class, arguments): thecreateWith
class method uses to create a new object instance(the util.createObjectWith is the same function).class
: the class constructor to create a new instance.arguments
(array): the arguments will be passed into the class constructor.
initialize(...)
: abstract initialization method after a new instance creating....
: the constructor's arguments should be passed into initialize method.
finalize
(...): abstract finalization method before the instance destroying....
: the free(destroy)'s arguments should be passed into finalize method.
- (deprecated)
init(...)
: abstract initialization method after a new instance creating.- init method is deprecated, pls use initialize method instead
...
: the constructor's arguments should be passed into init method.
- (deprecated)
final
(...): abstract finalization method before the instance destroying.- final method is deprecated, pls use finalize instead
...
: the free(destroy)'s arguments should be passed into final method.
free
(...): free the class instance....
: optional arguments will be passed into final method to process.
isIniting
(),isInited
(),isDestroying
(),isDestroyed
() object state testing methods:- to test object state
-
only for EventableObject:
- Methods:
dispatch
(event, args[, callback]): dispath an event or callbackevent
: the event nameargs
: the args are passed to event or callbackcallback
: optional, it will not dispatch event if the callback is exists, unless the callback return false.
dispatchError
(error[, callback]):error
: the error instance.callback
: optional, it will not dispatch'error'
event if the callback is exists, unless the callback return false.
- Events:
'initing'
: emit before the initialize method'inited'
: emit after the initialize method'destroying'
: emit before the finalize method'destroyed'
: emit after the finalize method
- Methods:
RefObject
RefObject has moved to ref-object
The RefObject
is derived from AbstractObject. and add the RefCount
and AddRef/Release
Supports.
- methods:
release()
/free()
: Decrements reference count for this instance. If it is becoming less than 0, the object would be (self) destroyed.addRef()
: Increments the reference count for this instance and returns the new reference count.
Usage:
AbstractObject = require'abstract-object'inherits = require'inherits-ex'createObject = AbstractObjectcreate#or createObject = require('inherits-ex/lib/createObject') inherits MyObjectAbstractObject : @cache = : -> @cache = null myObj = createObjectMyObject12 # if you do not wanna use `AbstractObject.create`, you MUST remember this: # even the constructor is empty, you should call the parent's constructor manually. # myObj = new MyObject() inherits MyObjectRefObject : -> # must call super method here: super : @cache = : -> @cache = null
the javascript:
var AbstractObject = var RefObject = var inherits = var createObject = AbstractObjectcreate//or var createObject = require('inherits-ex/lib/createObject') //if you do not wanna to use the 'AbstractObject.create'(createObject):var { //super call MyObject__super__constructor;}// or, this MUST use the 'AbstractObject.create'(createObject)var {} MyObjectprototype { thisa = a thisb = b thiscache = {}} MyObjectprototype { thiscache = null} var myObj = //or this, must overwrite the constructor and call the super constructor.var myObj = 12
AbstractError Classes
It has moved to abstract-error.
Enhanced Inheritance Functions
Moved to inherits-ex.
Util Functions
Moved to util-ex.