Skip to content

Commit 1fe33dd

Browse files
Update: added new files
1 parent cedee86 commit 1fe33dd

File tree

4 files changed

+221
-0
lines changed

4 files changed

+221
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// creating functions to create multiple objects
2+
3+
// proto , prottype , class --> will learn later
4+
5+
// normal way
6+
// const user = {
7+
// firstName: "Abhishek",
8+
// lastName: "Singh",
9+
// email: "singhabhishek@gmail.com",
10+
// age: 18,
11+
// address: "House Number, Colony, pincode, state",
12+
// about: function(){
13+
// return `${this.firstName} is ${this.age} years old.`;
14+
// },
15+
// is18: function(){
16+
// return this.age >= 18;
17+
// }
18+
// }
19+
// const aboutUser = user1.about();
20+
// console.log(aboutUser);
21+
22+
23+
// what if we needs millons of the user data then what we will do
24+
// hum bar bar user ki data ko input to nahi karenge so that's why
25+
// we will create function which will take user info data as inputs
26+
27+
// these is best way
28+
29+
// 1). function(that function create object)
30+
// 2). add key value pair
31+
// 3). object ko return krega
32+
33+
function createUser(firstName, lastName, email, age, address){
34+
const user = {};
35+
user.firstName = firstName;
36+
user.lastName = lastName;
37+
user.email = email;
38+
user.age = age;
39+
user.address = address;
40+
user.about = function(){
41+
return `${this.firstName} is ${this.age} years old.`;
42+
}
43+
user.is18 = function(){
44+
return this.age >= 18;
45+
}
46+
return user;
47+
}
48+
49+
const user1 = createUser('Abhishek', 'Singh', 'singh@gmail.com', '18', 'Mirzapur uttar pradesh');
50+
console.log(user1);
51+
52+
const is18 = user1.is18();
53+
console.log(is18); // true
54+
55+
const about = user1.about();
56+
console.log(about); // Abhishek is 18 year old.
57+
58+
// in these approach also have so setback(kameeyaa bhut hai esh me)
59+
// so will keep inproving it in next file(76).

JavaScript Part-01/76Extended75.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// jethne bar user banegga uthe ni baar methods banegaa our
2+
// hamare memory me uthni he methods store honge
3+
// user1 object banega usme bhi 'about' & 'is18' method honge
4+
// user2 object banega usme bhi 'about' & 'is18' method honge
5+
// user3 object banega usme bhi 'about' & 'is18' method honge
6+
// these is setback that we need to optimize
7+
// suppose ke 1 millon user hai to 1 million baar methods bhi store honge
8+
// memory me our memor jade use hoge we need to optimize it
9+
// we need only one copy of methods
10+
// javascript store it's references type
11+
12+
13+
// userMethod kar ke object create kar lenge
14+
// har object ke liye same hai methods
15+
// ek objects ke andar in methods ko store kar lenge
16+
// hum ek copy banake rakh lenge ur ush ko use karenge
17+
const userMethods = {
18+
about: function(){
19+
return `${this.firstName} is ${this.age} years old.`;
20+
},
21+
is18: function(){
22+
return this.age >= 18;
23+
}
24+
}
25+
function createUser(firstName, lastName, email, age, address){
26+
const user = {};
27+
user.firstName = firstName;
28+
user.lastName = lastName;
29+
user.email = email;
30+
user.age = age;
31+
user.address = address;
32+
// creating the referencs(address) of the 'about' & 'is18' function
33+
user.about = userMethods.about;
34+
user.is18 = userMethods.is18;
35+
return user;
36+
}
37+
38+
const user1 = createUser('Abhishek', 'Singh', 'singh@gmail.com', '18', 'Mirzapur uttar pradesh');
39+
const user2 = createUser('Abimaniyu', 'dev', 'singh@gmail.com', '16', 'prayagraj uttar pradesh');
40+
const user3 = createUser('prithivi', 'chauhan', 'singh@gmail.com', '24', 'varanasi uttar pradesh');
41+
const user4 = createUser('Raaj', 'Rajput', 'singh@gmail.com', '14', 'Delhi ');
42+
43+
console.log(user2.about());
44+
console.log(user4.about());
45+
46+
// in these code also have some kameeyaa we need to improve
47+
// need to optimize it improveing our code

JavaScript Part-01/77Extended76.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
// we improveing our code so that it takes less memory
3+
// suppose we want to add thousands of the method
4+
// and what if we forget to add it's reference in javascript
5+
// so, first we will understand in file "78.js"
6+
7+
const userMethods = {
8+
about: function(){
9+
return `${this.firstName} is ${this.age} years old.`;
10+
},
11+
is18: function(){
12+
return this.age >= 18;
13+
},
14+
sing: function(){
15+
return 'sa re ga ma....';
16+
}
17+
}
18+
function createUser(firstName, lastName, email, age, address){
19+
// using Object.create() function, these helps
20+
// in setting up the "userMethods" in "__proto__".
21+
// "__proto__" references hai "userMethods" kaa
22+
const user = Object.create(userMethods); // this line set "userMethod" in "__proto__" // understand in file 78
23+
user.firstName = firstName;
24+
user.lastName = lastName;
25+
user.email = email;
26+
user.age = age;
27+
user.address = address;
28+
user.about = userMethods.about;
29+
user.is18 = userMethods.is18;
30+
user.sing = userMethods.sing;
31+
return user;
32+
}
33+
34+
const user1 = createUser('Abhishek', 'Singh', 'singh@gmail.com', '18', 'Mirzapur uttar pradesh');
35+
const user2 = createUser('Abimaniyu', 'dev', 'singh@gmail.com', '16', 'prayagraj uttar pradesh');
36+
const user3 = createUser('prithivi', 'chauhan', 'singh@gmail.com', '24', 'varanasi uttar pradesh');
37+
const user4 = createUser('Raaj', 'Rajput', 'singh@gmail.com', '14', 'Delhi ');
38+
39+
console.log(user2)
40+
console.log(user2.about());
41+
// user methods are set in [[prototype]] or __proto__
42+
// "__proto__" me "userMethods" set hojaye gaa
43+
// "__proto__" is reference of "userMethod" Object.
44+
45+
// if we create thousand of the userMethods objects
46+
// and that are not persent in the "userCreate" object than
47+
// JavaScript will automatically will access that object
48+
// from '__proto__' which contents all that objects that are not in
49+
50+
console.log(user4.about());
51+
console.log(user3.sing());

JavaScript Part-01/78.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
const obj1 = {
3+
key1: "value1",
4+
key2: "value2"
5+
}
6+
// const obj2 = {
7+
// key3: "value3"
8+
// }
9+
// console.log(obj2.key3); // o/p--> value3
10+
// console.log(obj2.key1); // o/p--> undefined
11+
12+
// we want that javascript ko agar obj2 me 'key1' naa mile tho oo
13+
// khud 'obj1' ke paas jaye ohaa pee dekhe ki 'obj1' me 'key1' hai ke nahi hai to hume dede
14+
// khud 'obj1' ke pass jaye oha pe dekhe ki 'key1' hai to muje de de
15+
16+
17+
// "__proto__"
18+
19+
// offcial ecmascript documentation
20+
21+
// "[[prototype]]"
22+
23+
// "__proto__" , "[[prototype]]" --> both are same
24+
25+
// __proto__ ki madth se javaScript jo cheese 'obj2'
26+
// me nahi ho tho wo usko 'obj1' se lelega bcz 'obj1' is stored in "[[prototype]]"
27+
28+
// "prototype" --> this is diffrent from above two
29+
30+
// there is one more way to creat empty Object
31+
// Object.create();
32+
const obj2 = Object.create(obj1); // o/p--> {}
33+
// phale javaScript obj2 me dekhe ga agar wohaa pe hai ya nahi key
34+
// to wo ushko pher wshko obj1 me dekhe ga
35+
// Object.create();
36+
// this helps in stabiliting the connection b/w 'obj1' & 'obj2'.
37+
// this helps in creating 'obj1' properties in the 'obj2',
38+
// with help of javaScript "[[prototype]]" object
39+
// JavaScript ko jo chej 'obj2' se nahi mili to usne use 'obj1' se leli.
40+
41+
obj2.key3 = "value3";
42+
// obj.key2 = "uniqueKey";
43+
console.log(obj2.key2); //op--> value2
44+
45+
console.log(obj2);
46+
// o/p--> key3: "value3"
47+
// [[Prototype]]: Object
48+
49+
50+
// this is happening
51+
console.log(obj2.__proto__); // 'obj2' ko proto 'obj1' ho gaya
52+
// 'obj2' ko proto 'obj1' ho gaya with help of Object.create() function
53+
// const obj2 = Object.create(obj1);
54+
// o/p--> key1: "value1"
55+
// key2: "value2"
56+
// [[Prototype]]: Object
57+
58+
// JavaScript ko jo chej 'obj2' se nahi mili usne usko "__proto__" or "[[prototype]]" se leli jahaa pe obj1 stored hai.
59+
60+
// abhi humne bat ki __proto__ ki jishko documentation mein [[prototype]]
61+
// likha hua hai broweser ke console mei __proto__ bhi likha
62+
// ho saktha hai aur [[prototype]] bhi likha ho sakta hai
63+
// javaScript mai ek prototype property alag se bhi hoti hai
64+
// uske barein mai detail mei baat krenge

0 commit comments

Comments
 (0)