Skip to content

Commit 3dba09c

Browse files
feat: add objects
1 parent 6d5afb7 commit 3dba09c

File tree

1 file changed

+59
-0
lines changed
  • part6 (Object-Oriented Programming)/Objects

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
INFO: What are objects ?
3+
Objects are collection of key-value pairs where the key (also called properties) are strings (or symbols), and values can be any data type, including other objects, functions, arrays, etc.
4+
*/
5+
6+
7+
8+
/*
9+
INFO: Creating Objects
10+
There are multiple ways to create objects in JS, but the two most common are:
11+
1. Object Literal
12+
2. Using [ new Object() ]
13+
*/
14+
15+
// 1. Object Literal
16+
const person = {
17+
name: "Rafay",
18+
age: 17,
19+
greet: function() {
20+
console.log("Hello!");
21+
}
22+
};
23+
24+
// 2. Using new Object()
25+
const user = new Object();
26+
user.name = "Rafay";
27+
user.age = 17;
28+
user.greet = function() {
29+
console.log("Hello!");
30+
};
31+
32+
/*
33+
INFO: Accessing Properties and Methods
34+
you can access properties and methods in two ways:
35+
1. Dot notation
36+
2. Bracket notation
37+
*/
38+
39+
// 1. Dot notation
40+
console.log(person.name);
41+
person.greet();
42+
43+
// 2. Bracket notation
44+
console.log(person["age"]);
45+
person["greet"]();
46+
47+
/*
48+
INFO: Nested Objects
49+
Objects can have other objects as properties, creating nested structures.
50+
*/
51+
52+
const person1 = {
53+
name: "Rafay",
54+
address: {
55+
city: "New York",
56+
zip: 1001
57+
}
58+
};
59+
console.log(person.address.city);

0 commit comments

Comments
 (0)