Lua OOP implements chained invocation of an object

Source: Internet
Author: User

The chain rule in mathematics

http://sx.zxxk.com/ArticleInfo.aspx?InfoID=164649

Chain-Splitting rule:

The chain rule of the real number operation:

The chain rule for logarithmic operations:

The chain rule of parallel axioms:

Chain rules for vector operations:

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
ar obj = {    test : function(){         alert("Y");         return this;     },    test2 : function(){         alert("2");         return this;     }}obj.test().test2(); // And so on since it returns this

LUA OOP implementations

--

--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 ()

Lua OOP implements chained invocation of an object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.