-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Learned about call(), apply() and bind() method, some common mistakes…
… also
- Loading branch information
Showing
4 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
03 - Chai aur Javascript/14 - this Keyword/03_binding_this.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Binding this with call, apply and bind | ||
|
||
// JavaScript mein aap this ka value manually set kar sakte hain | ||
|
||
// call method | ||
|
||
function greet(){ | ||
console.log(`Hello ${this.name}`); | ||
} | ||
|
||
const person = {name: 'Sohail'} | ||
greet.call(person) // Hello Sohail | ||
|
||
|
||
// apply method | ||
greet.apply(person) // Hello Sohail | ||
|
||
|
||
// bind method | ||
const boundGreet = greet.bind(person) | ||
boundGreet() // // Hello Sohail |
17 changes: 17 additions & 0 deletions
17
03 - Chai aur Javascript/14 - this Keyword/04_classes_and_methods.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Classes and Methods | ||
|
||
class Person{ | ||
constructor(username){ | ||
this.username = username | ||
} | ||
|
||
greet(){ | ||
console.log(`Hello ${this.username}`); | ||
} | ||
} | ||
|
||
|
||
const person1 = new Person("Sohail") | ||
person1.greet() // Hello Sohail | ||
|
||
// Is mein greet method 'this.username' ko refer karta hai jo Person class ke instance ki property hai. |
63 changes: 63 additions & 0 deletions
63
03 - Chai aur Javascript/14 - this Keyword/05_common_mistakes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Common mistakes with 'this' | ||
|
||
// Loosing Context | ||
|
||
const person = { | ||
name: 'Sohail', | ||
greet: function(){ | ||
|
||
setTimeout(function() { | ||
console.log(`Hello ${this.name}`); | ||
}, 1000); | ||
} | ||
} | ||
|
||
person.greet() // Hello undefined | ||
|
||
// Solution | ||
|
||
const person2 = { | ||
name: 'Sohail', | ||
greet: function(){ | ||
|
||
setTimeout(() => { | ||
console.log(`Hello ${this.name}`); | ||
}, 1000); | ||
} | ||
} | ||
|
||
person2.greet() | ||
|
||
|
||
// Using this in Event listeners | ||
|
||
class Button{ | ||
constructor(element){ | ||
this.element = element | ||
this.element.addEventListener('click', this.handleClick.bind(this)) | ||
} | ||
|
||
handleClick(){ | ||
console.log("Button Clicked: ", this.element.id) | ||
} | ||
} | ||
|
||
const button = new Button(document.getElementById("myButton")); | ||
// Is example mein, handleClick method ko this ke correct context ke saath bind kiya gaya hai using bind method, taaki this.element event ke waqt correct button element ko refer kare. | ||
|
||
|
||
// Using this in Nested functions | ||
|
||
const obj = { | ||
value: 42, | ||
showValue: function(){ | ||
function innerFunction(){ | ||
console.log(this.value); | ||
} | ||
innerFunction.call(this) // 42 | ||
} | ||
} | ||
|
||
obj.showValue() | ||
|
||
// Yahan innerFunction.call(this) ka use context ko explicitly set karne ke liye kiya gaya hai, taaki this obj ko refer kare. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>this Keyword</title> | ||
<link rel="stylesheet" href="../style.css"> | ||
</head> | ||
<body style="background-color: #212121; color: white"> | ||
<nav> | ||
<a href="../index.html">Home Page</a> | ||
<a | ||
href="https://github.com/sohail019/Complete-Javascript/tree/main/03%20-%20Chai%20aur%20Javascript/04%20-%20Projects%20for%20DOM" | ||
>Github</a | ||
> | ||
</nav> | ||
|
||
<h1>this Keyword</h1> | ||
|
||
<!-- Event Handler --> | ||
<button id="myBtn">Click Me!</button> | ||
|
||
<!-- Common Mistakes --> | ||
<button id="myButton">Clicckkkkkkk</button> | ||
|
||
<script src="./01_this.js"></script> | ||
<script src="./05_common_mistakes.js"></script> | ||
</body> | ||
</html> |