Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views3 pages

JavaScript Objects: Creation & Usage

This document covers the concept of objects in JavaScript, explaining their structure as collections of key-value pairs and their significance in representing real-world entities. It details various methods for creating objects, accessing their properties, and using built-in object methods. Additionally, it introduces nested objects and demonstrates their usage with examples.

Uploaded by

Rajakumar Sai
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)
11 views3 pages

JavaScript Objects: Creation & Usage

This document covers the concept of objects in JavaScript, explaining their structure as collections of key-value pairs and their significance in representing real-world entities. It details various methods for creating objects, accessing their properties, and using built-in object methods. Additionally, it introduces nested objects and demonstrates their usage with examples.

Uploaded by

Rajakumar Sai
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

Web Technologies Unit-2

Objects in JavaScript:
An object is a collection of properties, where each property is a key-value pair.
Values can be data (like numbers or strings) or functions (called methods).
Objects are used to group related data and behavior together.

Why Use Objects?


To represent real-world entities like a `car`, `student`, or `employee`.
To organize and reuse structured data and functions.
Helps in object-oriented programming in JavaScript.

Syntax:
const person = {
name: "John",
age: 30,
isStudent: false,
greet: function() {
return "Hello, my name is " + [Link];
}
};

Explanation:
`name`, `age`, `isStudent` are properties.
`greet` is a method (a function inside an object).
`this` refers to the current object.

Creating Objects
1. Using Object Literal
const car = {
brand: "Toyota",
model: "Camry",
year: 2021
};

2. Using `new Object()`


const student = new Object();
[Link] = "Alice";
[Link] = 22;

3. Using Constructor Function


function Employee(name, id) {
[Link] = name;
[Link] = id;
Department of CSM AITAM, TEKKALI
Web Technologies Unit-2

}
const emp1 = new Employee("Ravi", 101);

4. Using Class (ES6)


class Book {
constructor(title, author) {
[Link] = title;
[Link] = author;
}

describe() {
return `${[Link]} by ${[Link]}`;
}
}
const b1 = new Book("1984", "George Orwell");

Accessing Object Properties

1. Dot Notation
[Link]([Link]); // Output: John

2. Bracket Notation
[Link](person["age"]); // Output: 30

Object Methods
A method is a function stored as a property.
const user = {
username: "admin",
login: function() {
return "User logged in";
}
};
[Link]([Link]()); // Output: User logged in

Looping Through Objects


Using `for...in` Loop

for (let key in person) {


[Link](key + ": " + person[key]);
}

Department of CSM AITAM, TEKKALI


Web Technologies Unit-2

Built-in Object Methods


Method Description
[Link](obj) Returns an array of property names
[Link](obj) Returns an array of property values
[Link](obj) Returns array of \[key, value] pairs
[Link]() Copies properties to another object

Nested Objects
Objects can contain other objects:
const student = {
name: "Priya",
address: {
city: "Hyderabad",
pincode: 500001
}
};
[Link]([Link]); // Output: Hyderabad

Department of CSM AITAM, TEKKALI

You might also like