This doesn’t result an error, and seems possible. How to do something like this?

What are you trying to do?

Sub function

Object(“”) – creates an object
Object(“”).set() – sets existing object

It’s still not clear what you’re trying to do (how come you tagged it jquery?). You need to give more information when posting a question.

If you want to access an existing object, you refer to it by the variable returned when you create the object (I’m sure you know this), eg

redEntity = Object("red player")

the second line should’ve been redEntity:set() (with a colon)

But, you seem to want to be able to refer to the object by the parameters you used to initialize it. In that case, perhaps an object-oriented approach is not what you really want. Perhaps you want a more functional approach, in other words, instead of having objects which retain state, you pass parameters into a function, and the function returns some new values, but otherwise doesn’t modify anything, doesn’t have any side-effects. So you could skip the initialization phase and just have

redEntity = setObject("red player", x, y)

whenever you need to set something.

I mean, if Object returns a class which has got the self.set() function in the class, can you do Object("").set()? Or do you need to post this:

X = Object("")

I just noticed I made a mistake in my post, I should’ve used a colon. I’ve corrected it.

We need to see some code, or at least know what you’re trying to do, or this is just guess work.

Instance methods

What does the set() method do? Does it require an instance of the Object class to operate? In that case, it’s an instance method, and it needs some way of knowing which instance it’s operating on. In Codea/Lua, the standard way of doing this is with colon notation:


This implicitly passes the instance to the method, where it becomes addressable as self (although the class system is implemented by Codea, the colon notation and self keyword is actually part of Lua).

If you wanted, you could invoke the method with dot notation, and include the instance as the first argument:

class.method(instance, arguments)

These two would have the exact same effect:

redEntity:set("hi there")
--is exactly the same as:
Object.set(redEntity, "hi there")


function Object:set(message)
    self.message = message

Most of the time though, there’s little point to doing this. redEntity:set() is more readable and allows you to take advantage of polymorphism more easily: in other words, you don’t need to know what class redEntity is, just as long as that class has a set method. So the redEntity instance could be some subclass of Object, and it wouldn’t matter. This is very useful when looping through an array of differently classed objects (as long as the objects all share the same interface, ie have the same method calls).

With the Object.set(redEntity, "hi there") call, you need to specify the class that the entity is. Part of the point of using OO is that you want polymorphism, you rarely want to have to specify this.

Calls to a super-class

About the only time you do want to specify the class is to call the superclass from an overridden method. Other languages have a super keyword for this, as Codea/Lua doesn’t:

subObject = class(Object) --a subclass of Object

--an override of the set method
function subObject:set(message)
     Object.set(self, message) -- call the superclass's version of this method
    --do whatever the subclass needs to do on top of this

Class methods

Now, it is possible that you might have a class where you have methods that do not operate on a particular instance, but rather on the entire class, or perhaps a set of those instances. In other languages these are called class methods or static methods. If they don’t need to access a particular ‘self’, then you could declare it with dot notation:

-- a class method, which doesn't have access to self:

function Object.iterateAll()
    --do something to Object class as a whole, or to an array of Objects, rather than to a specific self.
var = function(name)
    -- Object Table
    local varTab = {}
    varTab.create = function(varName)
        table.insert(library.pack.varObjects, {name = varName, type = name, val = {}})
    varTab.set = function(varType)
        print("Setting var " .. name .. ".type to " .. varType)
    varTab.print = function()
    return varTab

“var” are Object creating function.

So varTab is a table which has got some functions in it.

So Have

So Object("varType") returns the table with the functions, but will not do anything. And then Object("varType").create("id") will return the create function and run it. So you can form your code into this, and I think you can see there more things immediately.:


     When Object is a class:
     Object("class").set():init() --[[double column for classes]]