Skip to content

Commit d28ba5e

Browse files
committed
docs: Clarify Object Literals in JavaScript
- Solved the confusion between object literals and constructor-based object creation by clearly defining both concepts. - Explained that object literals create a new object each time they are declared, while constructor-based objects refer to the same singleton instance. - Covered key concepts including: - Declaration of objects using object literals with examples. - Accessing object properties using dot notation and bracket notation, highlighting their differences. - Modifying existing properties, adding new key-value pairs, and deleting properties from an object. - Demonstrated the use of Object.freeze() and Object.seal() methods to control object mutability. - Included examples of defining and invoking functions within an object.
1 parent 4e139e9 commit d28ba5e

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

02_Basics/02_objects2.js

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// There are two ways for declaring 'objects'
2+
// 1. using constructor (whenever u create an object using constructor then a singleton object is made.)
3+
// 2. using literal (in literals, singleton is not made.)
4+
5+
// REMEMBER:
6+
// 'Object literals' refers to the way of declaring an object.
7+
// When we declare an object using object literal, it is not a singleton object.
8+
// It is a new object every time we declare it.
9+
// But when we declare an object using constructor, it is a singleton object.
10+
// It means that whenever we declare an object using constructor, it is the same object every time. It is not a new object.
11+
// It is the same object that we declared first time.
12+
13+
// EXAMPLE 1: declaring an object named 'jsUser' then initializing the value.
14+
const jsUser = {
15+
// This '{}' is object and if we put values inside it then we can access accordingly.
16+
name: "Harshita", // key-value pair
17+
"full name": "Harshita pandey",
18+
age: 21,
19+
20+
occupation: "Software Engineer",
21+
isLoggedIn: false,
22+
lastLoginDays: ["Monday", "Saturday"], // array
23+
// We can also add function inside the object.
24+
sayHello: function () {
25+
console.log(
26+
"Hello, my name is " +
27+
this.name +
28+
" and I am " +
29+
this.age +
30+
" years old " +
31+
"working as a " +
32+
this.occupation +
33+
"."
34+
);
35+
},
36+
};
37+
// This is how you can access object
38+
console.log(jsUser.name); // accessing the value of 'name' key
39+
console.log(jsUser.age); // accessing the value of 'age' key
40+
// We can also access the array
41+
console.log(jsUser.lastLoginDays); // accessing the array
42+
// We can also access the function
43+
jsUser.sayHello(); // calling the function
44+
45+
// below mentioned gives the same output but have different significance
46+
console.log(jsUser.email);
47+
console.log(jsUser["email"]);
48+
// The above two lines of code are doing the same thing but the second one is more flexible.
49+
// The first one is more readable.
50+
// The second one is more flexible because we can use any string as the key.
51+
// console.log(jsUser.full name]); // wrong way, the compiler won't accept this
52+
console.log(jsUser["full name"]);
53+
54+
// This is how we change the key value
55+
jsUser["full name"] = "Harshita Vansh Mishra";
56+
console.log(jsUser["full name"]);
57+
58+
// This is how we can change the value
59+
jsUser.email = "[email protected]";
60+
console.log(jsUser.email);
61+
62+
// We can also add new key-value pair
63+
jsUser["city"] = "Mumbai";
64+
console.log(jsUser.city);
65+
66+
// We can also delete the key-value pair
67+
delete jsUser["full name"];
68+
console.log(jsUser["full name"]); // undefined
69+
console.log(["full name"] in jsUser); // false
70+
console.log(jsUser);
71+
72+
// We can use the Object.freeze() method to freeze the object. This will prevent the object from being changed.
73+
// We can use the Object.seal() method to seal the object so that no one could add new key-value pair but could modify the existing key-value pair.
74+
75+
/*
76+
// 1. Sealing the jsUser object
77+
Object.seal(jsUser);
78+
79+
// Attempting to add a new property (this will not work)
80+
jsUser["country"] = "India"; // This will not add the country property
81+
console.log("After seal, country:", jsUser.country); // Output: undefined (property not added)
82+
83+
// Modifying an existing property (this will work)
84+
jsUser.email = "[email protected]"; // This will change the email property
85+
console.log("After seal, email:", jsUser.email); // Output: "[email protected]"
86+
87+
// 2. Freezing the jsUser object
88+
Object.freeze(jsUser);
89+
90+
// Attempting to change a property (this will not work)
91+
jsUser["city"] = "Pune"; // This will not change the city property
92+
console.log("After freeze, city:", jsUser.city); // Output: "Mumbai" (remains unchanged)
93+
94+
// Attempting to add a new property (this will not work)
95+
jsUser["country"] = "India"; // This will not add the country property
96+
console.log("After freeze, country:", jsUser.country); // Output: undefined (property not added)
97+
98+
*/
99+
100+
// Adding a function to the jsUser object
101+
jsUser["greeting"] = function () {
102+
console.log("Hello, I am Harshita Vansh Mishra");
103+
};
104+
105+
// Logging the greeting function itself
106+
console.log(jsUser.greeting);
107+
// Output: [Function (anonymous)]
108+
// Explanation: This indicates that 'greeting' is a function and is defined in the jsUser object.
109+
// The function is an anonymous function expression because it does not have a name in its definition.
110+
// The name 'greeting' refers to the property in the jsUser object that holds this function.
111+
112+
// If you were to define the function with a name, it would look like this:
113+
jsUser["greeting"] = function greetingFunction() {
114+
console.log("Hello, I am Harshita Vansh Mishra");
115+
};
116+
// Now, if we log the function again:
117+
console.log(jsUser.greeting);
118+
// Output: [Function: greetingFunction]
119+
// Explanation: Here, 'greetingFunction' is the name of the function itself,
120+
// but it is still assigned to the 'greeting' property of the jsUser object.
121+
// The function can now be referenced by its name 'greetingFunction' internally,
122+
// but when accessed through the jsUser object, it is still referred to as 'greeting'.
123+
124+
// Calling the greeting function and logging its output
125+
console.log(jsUser.greeting());
126+
// Output:
127+
// Hello, I am Harshita Vansh Mishra
128+
// Explanation: This is the output from the console.log inside the greeting function,
129+
// which prints the greeting message when the function is called.
130+
131+
// Output: undefined
132+
// Explanation: This is the return value of the greeting function, which is undefined
133+
// because the function does not have a return statement.
134+
135+
// simple way
136+
jsUser.abc = function newfunction() {
137+
console.log("Hello.................");
138+
};
139+
jsUser.abc();
140+
141+
//------------------------------------------------------------------------------------------------
142+
// Example 2: object.create
143+
// This object.create uses constructor method wherein singleton is made.
144+
const jsUser2 = Object.create(null); // The Object.create() method creates a new object, or adds properties to the existing object.
145+
// We will later discuss in depth on this topic.
146+
//------------------------------------------------------------------------------------------------
147+

0 commit comments

Comments
 (0)