0% found this document useful (0 votes)
0 views3 pages

Module 6 JavaScript Object Reference - CS 112 - Javascript Prog - Minassian N. - FALL 2024 - SECTION# 16173

The document provides an overview of JavaScript objects, explaining their structure, properties, and methods through examples. It illustrates how to define an object, access its properties using the dot operator, and utilize the 'this' keyword for referencing object properties. Additionally, it covers the integration of objects with arrays and demonstrates how to manipulate and access object data within an array.

Uploaded by

jellycreamus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

Module 6 JavaScript Object Reference - CS 112 - Javascript Prog - Minassian N. - FALL 2024 - SECTION# 16173

The document provides an overview of JavaScript objects, explaining their structure, properties, and methods through examples. It illustrates how to define an object, access its properties using the dot operator, and utilize the 'this' keyword for referencing object properties. Additionally, it covers the integration of objects with arrays and demonstrates how to manipulate and access object data within an array.

Uploaded by

jellycreamus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Module 6 JavaScript Object Reference: CS 112 : Javascript Prog - Mina... https://ilearn.laccd.edu/courses/283068/pages/module-6-javascript-objec...

Module 6 JavaScript Object Reference


JavaScript Object Reference
An object in programming refers to a single entity with various properties and methods that are
related to the entity. It offers encapsulation of all related properties and methods in a single object
that can be easily accessed and it's methods executed.

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:

someObject.propertyName1 // returns the value stored under propertyName1

Taking the Person example above, let's define a person:

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...

Let's take a look at it's properties and methods:

• firstName, lastName and email are all Strings


• age is a Number
• fullName and sendEmail are both methods. since the object itself does not have a full name
property, we can construct a method that simply combines the first and last time to form the full
name and return it.
The this keyword

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:

alert("Hello " + person.firstName); // will output "Hello Amy"


alert(person.firstName + " is " + person.age + " years old"); // will output "Amy is 26 years old"
alert("Hello" + person.fullName()); // will output "Hello Amy Smith"
person.sendEmail(); // will hypothetically send an email to Amy

a. Objects and arrays

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.

// declare an empty array


var people = [];

// create a new person object


var person = {
name: "Nick",
age: 40
}

// insert the person object into the array


people.push(person);

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...

var people = [];

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:

alert(people[0].name); // Outputs "Nick"

Or using a for-loop:

// Outputs all names of the people object array


for(var i = 0; i < people.length; i++) {
alert(people[i].name);
}

3 of 3 10/28/2024, 5:32 PM

You might also like