Module 6 JavaScript Object Reference - CS 112 - Javascript Prog - Minassian N. - FALL 2024 - SECTION# 16173
Module 6 JavaScript Object Reference - CS 112 - Javascript Prog - Minassian N. - FALL 2024 - SECTION# 16173
A good example of an object can be a person. A person has properties such as name, age, dob,
address, email addresse, nick names, etc. A person can also have tasks such as waking up,
sleeping, exercising, working, sending email, etc. Therefore a person is a good candidate for an
object. Objects in JavaScript are defined as such:
var someObject = {
propertyName1: value1,
propertyName2: value2
};
Objects are defined by properties and methods encapsulated in curly braces. The property names
are arbitrary names that will represent the value that is stored under that name. They follow all the
syntax rules of variables. Property names are followed by a colon(:) for separation purposes
followed by values can be any of the JavaScript data types such as strings, numbers, arrays,
other objects, functions, etc.
Common Syntax Errors: The last property or method of the object should not end with a comma!.
This is a common syntax error to be careful for
Once the object has been defined, we can access it's property values using the dot (.) operator:
var person = {
firstName: "Amy",
lastName: "Smith",
age: 26,
email: "[email protected]",
fullName: function() {
return this.firstName + " " + this.lastName;
},
sendEmail: function() {
// Code that will hypothetically send an email
}
}
person is now referred to as an object with properties and methods relating to a person entity.
1 of 3 10/28/2024, 5:32 PM
Module 6 JavaScript Object Reference: CS 112 : Javascript Prog - Mina... https://ilearn.laccd.edu/courses/283068/pages/module-6-javascript-objec...
The this keyword in object-oriented programming refers to the instance or owner of the object that
the this keyword is used in. In other words, the object instance itself. If we take the fullName()
method in the person object example above, we notice that we use the this keyword to access the
firstName and lastName properties. During run-time, the this keyword will be equivalent to the
instance of the object itself. This is how we refer to object properties and methods while wiring
code inside the object itself.
We can now use the person's properties and methods to do something useful such as:
Objects are no different when combined with arrays than any other types you have seen so far.
We will start off my defining an array of people and insert a new person object. It's also good to
notice the grammar used in such situations. Object names should be singular and array names
should typically be plural.
Another way of inserting the new object into arrays is without storing it in a variable first. This is
useful if you just want to insert the object and have no reason to access it right away:
2 of 3 10/28/2024, 5:32 PM
Module 6 JavaScript Object Reference: CS 112 : Javascript Prog - Mina... https://ilearn.laccd.edu/courses/283068/pages/module-6-javascript-objec...
people.push({
name: "Nick",
age: 40
});
When it comes to accessing the objects in the array, we use the same structures like accessing
any other array element. Either using it's indexer directly:
Or using a for-loop:
3 of 3 10/28/2024, 5:32 PM