# Objects
Everything in JavaScript, which is not a primitive, is an Object.
However, there is a basic object that defines what Objects are and how they work. We will use the basic object to create our own custom objects very frequently.
Here are two examples of how you can create a basic Object. The first one is known as the literal syntax. It tends to be the one that we use. The second one is the Object constructor syntax. Both create Objects in the same way internally. There is no difference between these two Objects other than their variable names.
let myObj = {
prop: 'value',
name: 'Fred',
id: 12345,
isUseful: true,
};
let otherObj = new Object();
otherObj.prop = 'value';
otherObj.name = 'Fred';
otherObj.id = 12345;
otherObj.isUseful = true;
myObj.newProp = 'something';
otherObj.newProp = 'something';
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Dot Notation
Dot notation is a way to access objects inside of other objects as well as their properties and methods.
console.log( )
is an example you have already seen.
Here are a couple imaginary examples of dot notation:
Ottawa.Algonquin.mad9014.students.Dory.swim();
Ottawa.Algonquin.mad9014.students.Dory.speak('Just keep swimming');
2
Each dot separates the objects as you drill down through the objects to reach their properties or methods. Each object is acting as a container for the next object in the chain. Algonquin
exists inside of Ottawa
. mad9014
exists as an object inside of Algonquin
. There is a collection of objects called students
inside the mad9014
object... and so on.
swim( )
and speak( )
are both methods. Methods are actions that an object can carry out. We know that they are methods because they have a set of parentheses at the end of the name.