Objects are the fundamental building blocks of JavaScript. They act as containers that store collections of data (properties) and the associated actions (methods) that can be performed on that data. Understanding objects is crucial for creating interactive web pages, building complex applications, and structuring your JavaScript code effectively.
{}
. Properties are key-value pairs separated by colons. Methods are functions defined within the object.
const car = {
color: "red",
make: "Honda",
model: "Civic",
accelerate: function() {
console.log("Car is accelerating!");
}
};
console.log(car.color); // Output: red
car.accelerate(); // Output: Car is accelerating!
car
using curly braces {}
.color
(set to “red”), make
(“Honda”), model
(“Civic”), and a method named accelerate
.accelerate
method is a function defined within the object. It simply logs a message to the console when called.color
property of the car
object using dot notation (car.color
) and print its value (“red”) to the console.accelerate
method of the car
object using dot notation followed by parentheses (car.accelerate()
). This executes the function’s code, printing “Car is accelerating!” to the console..
) or bracket notation ([]
). Dot notation is preferred for simple property names.
const person = {
"first name": "Alice",
lastName: "Smith",
age: 30
};
console.log(person.age); // Output: 30
console.log(person["first name"]); // Output: Alice (using bracket notation)
person
with properties for first name, last name, and age.age
property using dot notation (person.age
) and print its value (30) to the console.first name
property using bracket notation (person["first name"]
). This is necessary because the property name has a space. It also prints “Alice” to the console.=
).
person.age = 31;
console.log(person.age); // Output: 31 (updated value)
age
property of the person
object. We use the same dot notation (person.age
) followed by the assignment operator (=
) and the new value (31).age
property again (console.log(person.age)
) to verify that it has been updated to 31.
const phone = {
number: "123-456-7890",
dial: function() {
console.log("Calling " + this.number); // `this` refers to the phone object
}
};
phone.dial(); // Output: Calling 123-456-7890
phone
with a number
property and a dial
method.dial
method is a function defined within the object. It uses string concatenation (+
) to create a message that includes the value of the number
property (this.number
). The this
keyword inside the method refers to the phone
object itself.dial
method of the phone
object using dot notation (phone.dial()
). This executes the function’s code and prints “Calling 123-456-7890” to the console.{}
.new Object()
constructor. While less frequently used in modern JavaScript, it can be helpful for creating multiple objects with similar properties and methods.
// Object literal
const book = {
title: "The Lord of the Rings",
author: "J.R.R. Tolkien",
pages: 1178
};
// Object constructor (less common)
const anotherBook = new Object();
anotherBook.title = "The Hitchhiker's Guide to the Galaxy";
anotherBook.author = "Douglas Adams";
anotherBook.pages = 184;
const student = {
name: "Bob",
age: 25,
address: {
street: "123 Main St",
city: "Anytown",
state: "CA"
}
};
console.log(student.address.city); // Output: Anytown
student
) with properties like name, age, and address.student.address.city
.
function Person(name, age) {
this.name = name;
this.age = age;
this.introduce = function() {
console.log("Hello, my name is " + this.name + ". I am " + this.age + " years old.");
};
}
Person
that acts as the blueprint (prototype) for creating person objects.Person
function takes two arguments: name
and age
.this
to set properties on the object being created (name
and age
).introduce
that logs a message to the console.
const student1 = new Person("Alice", 20);
const student2 = new Person("Bob", 22);
new
keyword with the Person
function to create new objects.student1
and student2
are instances (objects) inheriting properties and methods from the Person
prototype.new Person(...)
, JavaScript creates a new object, sets its internal prototype to the Person
function’s prototype, and then executes the Person
function with this
bound to the new object.
student1.introduce(); // Output: Hello, my name is Alice. I am 20 years old.
student2.introduce(); // Output: Hello, my name is Bob. I am 22 years old.
introduce
method on both student1
and student2
.introduce
is defined within the Person
function (prototype), both student1
and student2
can access and use it because they inherit from the Person
prototype.Objects are the foundation of complex data structures and applications in JavaScript. By mastering object creation, property access, methods, and advanced features like nested objects, you'll be well-equipped to build dynamic and interactive web experiences Happy coding !❤️