Skip to content

Object

The object data-type is for storing an unordered list of values. Each item in an object may be a different data type to other values in the object. However, once a value is inserted into an object, the normal rules apply about not being able to change it to another data-type.

The key thing that differentiates an object from an array, is the fact that an object's internal values are identified by names instead of numbers. For example, this is how we might retrieve a value from an object:

1
var result = myObject["one"];

An alternative way of achieving the same outcome is:

1
var result = myObject.one;

In this example, our object variable is called myObject and we are retrieving an element called "one", which we intend to store in a new variable called result


An object is a complex data-type which means that Modulo1 will avoid making copies of this kind of variable, as much as possible. And this behavior is quite common in modern programming languages, but it can be a source of confusion for programmers. So, if this concept sounds foreign to you, you need to pay close attention right now.

When a simple variable is used as the input to a function, Modulo1 makes a copy of that variable and sends the copy to become the input of the function. This means that if the function makes changes to the variable, it will not affect the original variable: the changes will only occur to the copy of the variable. For example:

1
2
3
4
5
6
7
var myAge = 32;

function double(number age) {
    age *= 2;
}
double(myAge);
print(myAge);

In this example, we use the variable myAge as the input to the function double(). Modulo1 creates a copy of myAge and then allows double() to refer to this copy as age. So, when double() performs calculations upon age, our original variable myAge is not affected. When we print myAge, we will see: 32.

On the other hand, when a complex variable is used as the input to a function, Modulo1 does not make a copy. Instead, Modulo1 sends the original version to the function. This means that if the function makes changes to the variable, it will affect the original variable. For example:

1
2
3
4
5
6
7
var penQuantities = { blue : 2, black : 4, red : 1 };

function addGreen(array pens) {
    pens.green = 2;
}
addGreen(penQuantities);
print(penQuantities);

In this example, we use the object penQuantities as the input to the function addGreen(). Modulo1 allows addGreen() to refer to the original version of penQuantities as cars. So, when addGreen() adds a new value to the end of cars, or original variable penQuantities is affected. When we print penQuantities, we will see: {blue : 2, black: 4, red: 1, green: 2}

This behavior also occurs when you save an object to a new location, such as into another object, into an array, or just under a new variable name. Simple variables will be duplicated, when saved to a new location, but complex variables will not. A consequence of this behavior, is that a complex variable can exist in an unlimited amount of locations at any one time. For example:

1
2
3
4
5
6
7
8
9
var pets = { dogs : 3 };
var pets2 = pets;
pets2.cats = 5;

var animals = { mammals : pets2 };
animals.mammals.fish = 2;

pets2["rabbits"] = 1;
print(pets);

In this example, we assigned our original object pets to many locations. We assigned it to pets2. Then we added cats to pets2. Next, we assigned pets2 to the mammals element of animals. Finally, we added "rabbits" to pets2. If pets had been a simple variable, then it would never have been changed in this example. A copy would have been made each time it was assigned to a new location and at each location the changes there would not affect any other locations. But instead, because Modulo1 recognises pets as an object, it means that its value was never copied. The exact same copy of the exact same value is being held by several variables. So, when one of these variables is changed, all of them are changed. When we print pets, we will see: {dogs : 3, cats : 5, fish : 2, rabbits : 1}


Syntax

To create a new object:

1
var pets = { totalDogs : 7, dogNames : ["spot", "fido", "bowser"] };

Spaces and line breaks are not important. The following achieves the exact same result:

1
2
3
4
var pets = { 
    totalDogs : 7, 
    dogNames : ["spot", "fido", "bowser"] 
};

To create a truly separate copy of an object:

1
var copyOfPets = object(pets);

To access an individual item from within an object:

1
var result = pets.totalDogs;

An alternative approach will yields the exact same outcome:

1
var result = pets["totalDogs"];

To access an element of an array that is within an object:

1
var result = pets.dogNames[1];

To create an object inside another object:

1
2
3
4
var tasks = { 
    morning : { breakfast : "make toast", exercise : "walking" },
    afternoon : { work : "programming", break : "walking again" }
};

To access an element of an object that is within another object:

1
var result = tasks.morning.breakfast;

An alternative approach that will yield the exact same outcome:

1
var result = tasks["morning"]["breakfast"];

To change an individual item within an object:

1
tasks.afternoon.work = "writing documentation";

To delete an object:

1
delete tasks;

To delete an individual item from within an object:

1
delete tasks.afternoon;

To add a new item to an object

1
tasks.afternoon.snack = "make popcorn";

To loop through all the main elements in an object:

1
2
3
for (var timePeriod in tasks) {
    print(timePeriod);
}

In this example, we ask Modulo1 to create a temporary variable called timePeriod which is of the iterator data-type. An iterator is a special kind of variable that does not hold any data of its own. Instead it points to other variables, which is especially useful when running loops. On each cycle of our loop, the iterator will point to a different element within our tasks object. This allows us to treat the iterator timePeriod as if it actually was that particular element of the tasks object. Anything we do to timePeriod, within this for loop, will actually be done to every element of tasks. Since there are two main elements within tasks, this block of code will cause the elements morning and afternoon to be printed in two lines, like this:

1
2
{ breakfast : "make toast", exercise : "walking" }
{ work : "writing documentation", break : "walking again", snack : "make popcorn" }

However, there will be cases where we actually want Modulo1 to loop through the inner elements contained inside the inner object. Here is how we would do that in this scenario:

1
2
3
4
5
for (var timePeriod in tasks) {
    for (var activity in timePeriod) {
        print(activity);
    }
}

In this example, the block of code will cause the inner elements to be printed like this:

1
2
3
4
5
"make toast"
"walking"
"writing documentation"
"walking again"
"make popcorn"

Properties

A property is a special feature of a variable that can be used to get and/or set some aspect of that variable.

length

The length property can be used to find out how many values are stored within a particular object variable. However if an object has arrays and objects inside it, their elements will not be added to the total.

1
var result = tasks.length;

type

The type property can be used to find out the data-type of a particular object variable. The result will always be "object"

1
var result = tasks.type;

Functions

There are currently no functions specific to objects. Come back later for updates