Earlier in the week, you learned about one type of collection storage - Arrays. Today, you will learn about another collection storage device call an Object. We use objects on a near daily basis as programmers and understanding how to build objects and how to retrieve information from an object can make your life as a programmer much easier.
When you are all done with the lessons, exercises, and questions for today, you will once again use git to save your work, and then put it in the cloud on Github.
Using your terminal, open the local copy of the repository that you created during setup. To do this, you will need to use the terminal command cd
to change into the directory that holds the repository. Once you are in the correct directory, use the terminal command atom .
to open the prework repository. If you are having trouble with this, see the day_1 instructions.
You are now going to learn about the Object data structure in Javscript. An object is a way to store data just like an array, but with several key differences. An array stores an ordered list of data. Each item in the array can be accessed using only numbers, or the index of item. For example:
var animalsArray = ["Zebra", "Giraffe", "Elephant"];
animalsArray[0]; // Accesses the first item in my array, "Zebra"
animalsArray[1]; // Accesses the second item in my array, "Giraffe"
animalsArray[3]; // Accesses the third item in my array, "Elephant"
Objects store data but instead of using only numbers to get the data, you can use strings. This lets you treat an object like it's a database for storing and organizing data. Below is the syntax of an object. This is called Object Literal.
var zebraObject = {
name: "Angela",
age: 3,
height: "5ft"
};
In the zebraObject
, we store information about the Zebra. This object contains 3 properties that consist of key-value pairs. In zebraObject
the first property is name: "Angela"
. The key of this property is name
. The value of this property is "Angela"
. The value of a property can hold any data type. Let's go ahead and add a new property with a key of diet
and a property that is an array.
var zebraObject = {
name: "Angela",
age: 3,
height: "5ft",
diet: ["grass", "leaves", "water"]
};
Functions can also be stored as a value in a key-value pair. In these cases, we refer to the function as a method.
var zebraObject = {
name: "Angela",
age: 3,
height: "5ft",
diet: ["grass", "leaves", "water"],
feed: function() {
console.log("Give grass to the Zebra");
}
};
Methods can be called using the dot or bracket notation, however, you'll mostly see dot notation.
zebraObject.feed(); //logs "Give grass to the Zebra"
-
Work through these resources to gain some repetition with reading and working with objects.
- Read through the Object Literals lesson plan and work on the final practice provided at the end.
- Read pages 100 through 107 in the JAVASCRIPT & JQUERY: interactive front-end web development book.
- Note: You will come across the keyword
this
in the reading.this
refers to the object that thethis
is contained in. The example in the reading uses ahotel
object, wherecheckAvailability()
usesthis.rooms
andthis.booked
.this.rooms
refers to the value that matches the keyrooms
within the object it is contained in, which is40
.this.booked
refers to the value that matches the keybooked
within the object it is contained in, which is25
.this
is a concept that we will cover more thoroughly in class. This brief overview is provided for your comprehension of the reading.
- Note: You will come across the keyword
- Read through the objects-ppt.pdf. This is an excerpt from Code School.
-
Answer the questions in the
questions.md
file in the day_5 directory. -
Work through the
objects.js
file in the day_5/exercises directory.
When you are finished with all of the day_5 activities, use your terminal to run the following commands in order to save your work to your local git repository.
$ git add day_5/exercises
$ git add day_5/questions.md
- Use
git add day_5/<filename>
to add all additional files that you created today $ git status
- you should see only green filenames - if you see any that are red, continue togit add
those files untilgit status
shows all green files.$ git commit -m "Add Day 5 Work"
You've save your work to git on your local machine, but it is not yet accessible through your remote Github repository. Updating our remote Github repository with our local changes is called pushing. Push your code with the following command:
git push origin master
You should now be able to log in to GitHub, navigate to your remote prework repository and see all the work you did today!