Object
An Object
is container of values where each value is associated to a key.
Values are also called properties or fields.
Creating objects
An Object
can be created using curly braces, for example you can create an empty object with:
An Object
can be initialized with some string keys/values:
If the key is a string made up of alphanumeric characters (plus underscores) then the double quotes can be omitted:
Reading values
If you have the key, you can read the value associated to it by using the dot notation .
:
Values can also be read using the array access operator []
, in this case you can use any valid expression to compose the key:
Writing values
Values can be written using both the dot notation .
and the array access operator []
:
Keys from expressions
Keys can also be initialized from an expression using brackets in the key definition:
Non-string keys
Keys are not restricted to keys only, they can also be any of the other types.
Booleans
, Integers
, Numbers
and Strings
will compare by value and can be defined using the brackets [key]
syntax explained above:
Reference keys
A reference can also be used as key in an object, such as a reference to a Closure
, an Array
, another Object
, and so on. When this is done, the address of the reference will be used as the key, this means that you have to use the exact same instance to get the value back:
Symbol keys
A Symbol
key will match only a reference to itself. Using a Symbol
as an object's key can be used to control the accessibility of an Object
's value and to avoid accidental collisions between keys:
note
This technique is used extensively throughout melon's core and modules. One example is operator overloading.
Iterating over keys
If you retrive the Iterator
from an Object
using the iterator operator >
, you can use this iterator to iterate over the keys of an Object
, for example in a for-in
loop.
Prototype
Objects
can be chained together by making one a prototype for the other. This is done using the bless operator @
:
When an attempt to read a non existing property is made on an object, the object's prototype (if any) will be searched for the missing property and, if found, the prototype's property's value will be returned instead of null
.
This lookup is performed recursively on the whole prototype chain until an object with no prototype is found. If the requested key is not found on any of the prototypes the resulting value will be null
.
note
This mechanism can be used to do OOP, specifically prototype based programming.
Method access operator
As we know, an Object
can contain a Function
as one of its values. Most OOP languages provide the concept of a method, which is a function which is invoked on an object and is able to alter its internal state by using a reference to the object on which it's invoked.
In melon we can achieve a similar result with:
The myObj
in the example above si clearly redundant so melon provides syntactic sugar to make it more expressive:
Basically the method access ->
operator silently passes the object on which it is being used as the first parameter of the closure you are accessing with it.
See also Function
for a better understanding of the this
keyword and of ->
vs =>
when creating closures.
Further object manipulation
There are other ways to manipulate Object
values, they can be found in the object
core module.