JavaScript (JS) objects are one of the core data structures used to store and manage data.
They represent collections of key-value pairs, where:
Keys are strings (or Symbols).
Values can be any type: string, number, function, array, another object, etc.
Eg:-
const person =
{ name: "Alice",
age: 30,
isEmployed: true,
greet: function() {
[Link]("Hell
o!");
};
Accessing Properties
[Link]([Link]); // Dot notation → "Alice"
[Link](person*"age"+); // Bracket notation → 30
Bracket notation is useful for dynamic property access:
const key = "isEmployed";
[Link](person[key]); // true
◻ Modifying & Adding Properties
[Link] = 31; // Modify existing
[Link] = "New York"; // Add new
◻ Deleting Properties
delete [Link];
◻ Nested Objects
const company =
{ name: "Tech
Corp",
address: {
city: "San Francisco",
zip: "94107"
}
};
[Link]([Link]
[Link]); // "San
Francisco"
◻ Looping Through
Properties
for (let key in person)
{ [Link](key + ": " +
person[key]);
}
◻ Object Methods
Built-in methods to interact with objects:
[Link](person); // ["name", "age", "greet"]
[Link](person); // ["Alice", 30, function]
[Link](person); // [["name", "Alice"], ["age", 30], ["greet",
function]]
◻ Object Destructuring
const { name, age } = person;
[Link](name, age); // "Alice", 30
1. Methods in JavaScript
A method is just a function that is a property of an object.
◻ Example:
const car =
{ brand:
"Toyota",
start: function()
{
[Link]("Eng
ine
started.");
},
drive()
{ [Link]("Driving..."
);
}
};
[Link](); // Engine started.
[Link](); // Driving...
start and drive are both methods of
the car object.
2. Constructor Functions
A constructor is a function used to create multiple instances (objects) with the same
structure and behavior.
◻ Basic Constructor Function:
function Person(name, age) {
[Link] = name;
[Link] = age;
[Link] = function()
{ [Link](`Hi, I'm $
{[Link]}`);
};
}
const person1 = new Person("Alice", 30);
const person2 = new Person("Bob", 25);
[Link](); // Hi, I'm Alice
[Link](); // Hi, I'm Bob
new keyword creates a new object, sets
this to it, and returns it.
◻ ◻ Avoiding Duplication with Prototypes
Putting methods on the prototype is more memory-efficient:
function Person(name, age) {
[Link] = name;
[Link] = age;
}
[Link] = function()
{ [Link](`Hi, I'm $
{[Link]}`);
};
const p1 = new Person("Charlie", 40);
[Link](); // Hi, I'm Charlie
JavaScript Object Properties
There are two types of properties in JavaScript:
1. Data properties
2. Accessor properties (getters/setters)
And then there's the prototype, which is a core part of how objects inherit properties and
methods.
1. Data Properties
These are the most common type — they hold a static value.
◻ Example:
const person = {
name: "Alice",
age: 30
};
[Link](pers
[Link]); //
"Alice"
Here, name and age
are data
properties.
Each data property
has 4 attributes
(invisible unless
you inspect them):
value: The
actual value
("Alice",
30)
writable:
If the value
can be
changed
enumerable
: If it shows
up in loops
like
for...in
configurab
le: If the
property can
be deleted
or changed
You can define
them manually:
[Link](person, "gender", {
value: "female",
writable: true,
enumerable: true,
configurable: true
});
2. Accessor
[Link] = "Jane Smith";
[Link]([Link]); // "Jane"
[Link]([Link]); // "Smith"
Key Difference:
Data property → stores value
Accessor property → calculates or sets
value using a function
You can also define them using
[Link]:
[Link](user, "fullName", {
get: function() {
return `${[Link]} $
{[Link]}`;
},
set: function(value) {
[[Link], [Link]] =
[Link](" ");
}
}
)
;
3.
Pr
ot
oty
pe
Eve
ry
Jav
aSc
ript
obj
ect
has
an
inte
rnal
lin
k to
ano
ther
obj
ect
call
ed
its
pro
tot