Skip to content

Commit cedee86

Browse files
update: files
1 parent 4a3fe7b commit cedee86

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// call apply bind
2+
3+
function hello() {
4+
console.log("hello world");
5+
}
6+
// normally calling a function
7+
hello(); // o/p --> hello world
8+
// both will same output
9+
// using .call() function --> we can call a function
10+
hello.call(); // o/p --> hello world
11+
12+
// ex
13+
const user = {
14+
firstName : "Abhishek",
15+
age : 21,
16+
about: function(){
17+
console.log(this.firstName, this.age)
18+
}
19+
}
20+
user.about(); // o/p --> Abhishek 21
21+
22+
23+
// explaining call() function
24+
function about(hobby, favMusician){
25+
console.log(this.firstName, this.age, hobby, favMusician);
26+
}
27+
const user1 = {
28+
firstName: "abhishek",
29+
age: 18,
30+
}
31+
const user2 = {
32+
firstName: "Prthivi",
33+
age: 16,
34+
}
35+
// hume user2 keliye about() use karna hai
36+
// these can be possible with the help of call() function
37+
about.call(user2); // o/p--> Prthivi 16 undefined undefined
38+
// user2 ko borrow karna hai user1 se about().
39+
about.call(user1, "guitar", "moazrt");
40+
// o/p --> abhishek 18 guitar moazrt
41+
// in call we have pass it as a String
42+
43+
44+
// apply
45+
// in appiy we pass in arguments in form of array
46+
about.apply(user1, ["Drum", "bach"]);
47+
// o/p --> abhishek 18 Drum bach
48+
49+
// bind
50+
// bind() return function which can be stored and called
51+
const func = about.bind(user2, "instuments", "jagjitJi");
52+
// o/p --> Prthivi 16 instuments jagjitJi
53+
func();
54+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
3+
const user1 = {
4+
firstName: "Abhishek",
5+
age: 18,
6+
about: function(){
7+
console.log(this.firstName, this.age);
8+
}
9+
}
10+
11+
// don't do this mistakes
12+
// these not right way here we call we need to bind
13+
const myFunc = user1.about;
14+
15+
// user1.about();
16+
17+
//const myFunc = user1.about.bind(user1);
18+
myFunc();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// arrow function
2+
3+
// arrow function me this nahi hotha
4+
// arrow function ka "this" jo hota usho oh apne surrounding's se letha hai
5+
// 'this' lega ek level up yani ke 'window' hoga es case me
6+
7+
const user1 = {
8+
firstName: "Abhishek",
9+
age: 18,
10+
about: () => {
11+
console.log(this.firstName, this.age);
12+
}
13+
}
14+
// user1 will not be "this" for arrow function
15+
// this ek level up-hogaa matlb--> "window" hogaa
16+
user1.about(); // op--> undefined undefined
17+
18+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// short syntax
2+
3+
// const user = {
4+
// firstName: "Abhishek",
5+
// age: 18,
6+
// about: function(){
7+
// console.log(this.firstName, this.age);
8+
// }
9+
// }
10+
11+
// object ke andar jo function hote hai unko methods kah te hai
12+
13+
// both above and below codes are same
14+
const user1 = {
15+
firstName: "Abhishek",
16+
age: 18,
17+
about(){
18+
console.log(this.firstName, this.age);
19+
}
20+
}
21+
user1.about();

0 commit comments

Comments
 (0)