Object
Object are really useful in javascript they allow you to structure your code and better organize your application. Object can interact each others through properties and methods with the inheritance check this course to learn more about inheritance
Create an object
Create an object is really simple there are many methods to do that but in this courses we will use the most used:
const name = {};
- To create an
object
simply create avariable
. - After the = open curly braces, the computer will understand that you create an object.
Properties in object
Object
are like arrays
you can store many properties that you can call later to create a properties:
const name = {
firstName: "lucas",
lastName: "Tostée",
age: 22,
friends: ["keke","gigi"],
stuff: {}
}
- The first thing for create a property is to give a name followed by
:
, it's important to use double point instead of = when you are in object you have to use this syntax. - Each properties is ending by a
,
which tell to the computer this properties is done you can switch to another one. - You may notice that you can stores anything that you want in properties like
string
,number
,array
etc.. and even other object.
Access properties
Once you create your properties you maybe want to access it or modify etc.. ? Let's see how you can do that:
const name = {
firstName: "lucas",
lastName: "Tostée",
age: 22,
friends: ["keke","gigi"],
stuff: {}
}
return name.firstName; // Lucas
return name["age"]; // 22
return name.friends[0]; // keke
return name.lucas = "Hugo";
- There are two ways of access
properties
in object, the first one and the most used is the dot notation. - Simply call the name of your object and use
.
followed by your properties name. - The second method is with
[]
. - Repeat the same operation than the dot notation but use
["propertiesName"]
after your object name. - If you want modify the content of a properties you can use = and the value that you want to modify the property.
Methods in object
From now we saw how to stores informations in an object but what if we want create function
inside object. Let's see how you can create methods
:
const name = {
firstName: "lucas",
yearBirth: 1995,
calcAge: function () {
return 2018 - this.yearBirth;
}
}
- A lot of new things here, so first a function in object is called a
method
and you can create a method just like properties but used the wordfunction
after the double points.
Note - The new keyword
this
is a little bit strange for now but check this course for more information about this.
Access methods
const name = {
firstName: "lucas",
yearBirth: 1995,
calcAge: function () {
return 2018 - this.yearBirth;
}
}
name.calcAge;
- To call a method use the same method than properties but you may notice that methods don't need to use
()
for calling her, instead of regular function you can avoid to use()
.
Current errors
This is a list of the main errors that you can meet when you use Object:
Note: I'm not a wizard there is maybe some issue that you notice above so fell free to open an issue in the github repo if you find a new error not mentioned above.