Skip to content

Commit b114beb

Browse files
committed
bind keyword in js
1 parent 0a21694 commit b114beb

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed

10_classes_ans_oop/bind.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>React</title>
7+
</head>
8+
<body>
9+
<button>Button Clicked</button>
10+
</body>
11+
<script>
12+
class React {
13+
constructor(){
14+
this.library = "React"
15+
this.server = "https://localhost:300"
16+
17+
//requirement
18+
document
19+
.querySelector('button')
20+
.addEventListener('click', this.handleClick.bind(this))
21+
22+
}
23+
handleClick(){
24+
console.log("button clicked");
25+
console.log(this.server);
26+
}
27+
}
28+
29+
const app = new React()
30+
</script>
31+
</html>

10_classes_ans_oop/call.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function SetUsername(username){
2+
//complex DB calls
3+
this.username = username
4+
console.log("called");
5+
}
6+
7+
function createUser(username, email, password){
8+
SetUsername.call(this, username)
9+
10+
this.email = email
11+
this.password = password
12+
}
13+
14+
const chai = new createUser("chai", "chai@fb.com", "123")
15+
console.log(chai);

10_classes_ans_oop/inheritance.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class User {
2+
constructor(username){
3+
this.username = username
4+
}
5+
6+
logMe(){
7+
console.log(`USERNAME is ${this.username}`);
8+
}
9+
}
10+
11+
class Teacher extends User{
12+
constructor(username, email, password){
13+
super(username)
14+
this.email = email
15+
this.password = password
16+
}
17+
18+
addCourse(){
19+
console.log(`A new course was added by ${this.username}`);
20+
}
21+
}
22+
23+
const chai = new Teacher("chai", "chai@teacher.com", "123")
24+
25+
chai.logMe()
26+
const masalaChai = new User("masalaChai")
27+
28+
masalaChai.logMe()
29+
30+
console.log(chai instanceof User);

10_classes_ans_oop/myClasses.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// ES6
2+
3+
class User {
4+
constructor(username, email, password){
5+
this.username = username;
6+
this.email = email;
7+
this.password = password
8+
}
9+
10+
encryptPassword(){
11+
return `${this.password}abc`
12+
}
13+
changeUsername(){
14+
return `${this.username.toUpperCase()}`
15+
}
16+
17+
}
18+
19+
const chai = new User("chai", "chai@gmail.com", "123")
20+
21+
console.log(chai.encryptPassword());
22+
console.log(chai.changeUsername());
23+
24+
// behind the scene
25+
26+
function User(username, email, password){
27+
this.username = username;
28+
this.email = email;
29+
this.password = password
30+
}
31+
32+
User.prototype.encryptPassword = function(){
33+
return `${this.password}abc`
34+
}
35+
User.prototype.changeUsername = function(){
36+
return `${this.username.toUpperCase()}`
37+
}
38+
39+
40+
const tea = new User("tea", "tea@gmail.com", "123")
41+
42+
console.log(tea.encryptPassword());
43+
console.log(tea.changeUsername());

10_classes_ans_oop/staticprop.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class User {
2+
constructor(username){
3+
this.username = username
4+
}
5+
6+
logMe(){
7+
console.log(`Username: ${this.username}`);
8+
}
9+
10+
static createId(){
11+
return `123`
12+
}
13+
}
14+
15+
const hitesh = new User("hitesh")
16+
// console.log(hitesh.createId())
17+
18+
class Teacher extends User {
19+
constructor(username, email){
20+
super(username)
21+
this.email = email
22+
}
23+
}
24+
25+
const iphone = new Teacher("iphone", "i@phone.com")
26+
console.log(iphone.createId());

0 commit comments

Comments
 (0)