-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path034-objects.js
More file actions
100 lines (83 loc) · 1.94 KB
/
034-objects.js
File metadata and controls
100 lines (83 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// 1: WHAT ARE OBJECTS?
const person = {
name: "John",
age: 25,
isStudent: true
};
// 2: WHY USE OBJECTS?
const personName = "John";
const personAge = 25;
const personIsStudent = true;
// 3: OBJECT LITERAL - THE STANDARD WAY
const car = {
brand: "Toyota",
model: "Corolla",
year: 2022,
color: "Silver"
};
console.log(car);
const book = {};
console.log(book); // Empty object
// 4: OBJECT CONSTRUCTOR - THE OLD SCHOOL WAY
const student = new Object();
student.name = "Sarah";
student.age = 22;
student.major = "Computer Science";
console.log(student);
const teacher = new Object({
name: "Mike",
subject: "Math"
});
// 5: LITERAL VS CONSTRUCTOR - WHICH ONE TO USE?
// Object Literal - Modern, Preferred
// const product1 = {
// name: "Laptop",
// price: 999
// };
// Object Constructor - Old School, Avoid
// const product2 = new Object();
// product2.name = "Laptop";
// product2.price = 999;
// 6: PRACTICAL EXAMPLES
// Example 1: User Profile
const user = {
username: "john_doe",
email: "john@example.com",
age: 28,
isActive: true,
followers: 1250
};
console.log(user);
// Example 2: Product in E-commerce
const product = {
id: 101,
title: "Wireless Headphones",
price: 79.99,
inStock: true,
rating: 4.5
};
console.log(product);
// Example 3: Blog Post
const post = {
title: "Introduction to JavaScript Objects",
author: "Jane Smith",
date: "2025-10-19",
views: 3420,
published: true
};
console.log(post);
// 7: OBJECTS CAN HOLD ANYTHING
const company = {
name: "Tech Solutions",
founded: 2020,
employees: 150,
departments: ["Engineering", "Sales", "HR"],
ceo: {
name: "Alice Johnson",
age: 45,
experience: 20
}
};
console.log(company);
console.log(company.departments); // Array inside object
console.log(company.ceo.name); // Nested object property