Objects

An object is a collection of key-value pairs.

Create an object#

To create an object, use curly braces {}.

Empty object#

object = {}

Object with properties#

person = {
    name "John"
    age 30
}

Nested object#

An object can contain other objects:

person = {
    name "John"
    contact {
        email "[email protected]"
        phone "123456789"
    }
}

Object with array#

person = {
    name "John"
    skills ["programming" "design"]
}

Read properties#

To access a property, use the dot .:

person = {
    name "John"
    age 30
}

print person.name  # John
print person.age    # 30

For nested properties, chain the dots:

person = {
    contact {
        email "[email protected]"
    }
}

print person.contact.email  # [email protected]

Modify properties#

To change a value, assign a new value:

person = {
    name "John"
    age 30
}

person.name = "Mary"
person.age = 25
print person.name  # Mary

Dynamic properties#

You can add new properties simply by assigning them:

person = {
    name "John"
}

person.age = 30
person.city = "Madrid"
print person.age     # 30
print person.city   # Madrid

For properties with dynamic names (variables), use square brackets [] as in arrays:

person = {
    name "John"
}

key = "lastname"
person[key] = "Perez"
print person.lastname  # Perez
Number of properties

The len property indicates how many properties the object has. Methods like set or delete to add and remove elements are on the Object methods page.