JS Object Chain Call Method http://stackoverflow.com/questions/15029309/ how-to-write-jquery-chainable-functions-for-local-usinghttp://stackoverflow.com/questions/15029309/ How-to-write-jquery-chainable-functions-for-local-using
--
--Class Helper Routines
--
--Instantiates a class
Local function _instantiate (class, ...)
Local Inst = setmetatable ({__class=class}, {__index = class})
If Inst.__init__ Then
Inst:__init__ (...)
End
Return Inst
End
---Create a Class object (Python-style object model).
--The class object can be instantiated by calling itself.
--Any class functions or shared parameters can is attached to the this object.
--Attaching a table to the class object makes this table shared between
--all instances of the This class. For object parameters use the __INIT__ function.
--Classes can inherit member functions and values from a base class.
--Class can is instantiated by calling them. All parameters'll be passed
--The __init__ function of this class-if such a function exists.
--The __INIT__ function must is used to set any object parameters that is not shared
--with the objects of this class. Any return values would be ignored.
--@param base the base class to inherit from (optional)
--@return A class object
--@see instanceof
--@see Clone
function Class (Base)
--__parent Property Cache parent class for child class Index parent class method
Return setmetatable ({__parent = base}, {
__call = _instantiate,
__index = Base
})
End
---Test whether the given object is an instance of the given class.
--@param Object Object instance
--@param class class object to test against
--@return Boolean indicating whether the object is an instance
--@see Class
--@see Clone
function Instanceof (object, Class)
Local meta = getmetatable (object)
While Meta and Meta.__index do
If Meta.__index = = Class Then
return True
End
Meta = getmetatable (Meta.__index)
End
return False
End
Local chainclass = Class ()
Chainclass.setflag = function (self, flag)
Self.flag = Flag
Print ("Set flag =": Flag
return self
End
Chainclass.settype = function (self, type)
Self.type = Type
Print ("Set type =": Type
return self
End
Chainclass.printattrs = function (self, flag)
For k,v in pairs (self) do
Print ("Name="): K.: "Value=". ToString (v))
End
return self
End
Local chainobj = Chainclass ()
Chainobj:setflag (1): SetType (2):p rintattrs ()