1+ // Object literal -> Simply an Object
2+ const user = {
3+ username : "Hitesh" ,
4+ loginCount : 8 ,
5+ isLogged : true ,
6+
7+ getUserDetails : function ( ) {
8+ // console.log("Got user details hide form you!")
9+ console . log ( `Username: ${ this . username } ` )
10+ // if we not use it it
11+ // give me an error cause fucntion don't kown what user name is
12+ // although it is define in the scope but it cannont accessing
13+ // that's why we use the this
14+ console . log ( this ) // shows you the current constest or the current object user which is pointed by the this
15+ }
16+ }
17+
18+ // console.log(user.username)
19+ // console.log(user.getUserDetails())
20+
21+ // This keyword => it is basically a current contest in the scope
22+ // you konw that writing this in the global give you empty or in browser gives you the window object
23+
24+ // Constructor Function
25+
26+ function UserOne ( username , loginCount , isLogged ) {
27+ // this is used with the variable
28+ this . username = username ;
29+ this . loginCount = loginCount ;
30+ this . isLogged = isLogged
31+
32+ return this
33+ // return nahi bhi hoga to bhi ye return to hoga cause return is implecity define
34+ }
35+
36+ const user1 = UserOne ( "Hietsh" , 12 , true ) ;
37+ // const user2 = UserOne("Chai Aur Code", 11, false) // It overwrite the values that is not the good thing in the code ...
38+ console . log ( user1 ) // it will show you but,
39+
40+ // constructor function ik function ki copy deta hai ik instance deta hai uss ka use kar ke duasre koi usse affect nahi hote
41+ const user3 = new UserOne ( "JS" , 23 , true )
42+ console . log ( user3 ) ;
43+
44+ // new working -> if you use new keyword then there is creation of empty object called instance
45+ // 2) then, construction function call hota hai new keyword ke wajah se ye aapke jitn bhi argument ko wrap karte hai object ke andar
46+ // 3) this keyword ke andar argument inject ho jate hai
47+
48+ console . log ( UserOne . constructor )
49+ // it is nothing but the self reference of the any function
50+
51+ // instanceOf
52+ console . log ( user1 instanceof UserOne ) // False -> cause we don't use the new so it not create the instance of the object..
53+ console . log ( user3 instanceof UserOne ) // True
54+ console . log ( user3 instanceof Object ) // True
0 commit comments