Skip to content

Commit c140386

Browse files
committed
oops
1 parent 4ba1594 commit c140386

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

15_OOPs/notes.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Javascript and Classes
2+
3+
Yes, Javascript does have classes. But, they are just the syntactic sugar cuase the javscript is prototype-based langauge and it works in existing prototype-based inheritance mechanisms..
4+
5+
## OOP
6+
7+
## Object
8+
- Collection of properties and methods
9+
- toLowerCase
10+
11+
## why use OOP
12+
- code is getting mess-up (spegaty code)
13+
- we can use different feature like sevices
14+
15+
## Parts of OOPs
16+
Object literal
17+
18+
- Constructor function
19+
- Protype
20+
- Classes
21+
- Instances (new, this)
22+
23+
# 4 Pillors
24+
- Abstraction -> Hiding details from unneccessary users (e.g., fetch, network calls)
25+
- Inheritance ->
26+
- Encapsulation -> A wrapper
27+
- Polymorphism

15_OOPs/oops.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

Comments
 (0)