forked from bittu1040/JavaScript-Coding-and-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall-apply-bind.js
More file actions
62 lines (41 loc) · 2.03 KB
/
call-apply-bind.js
File metadata and controls
62 lines (41 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// call() is one of the ways to control the value of `this` within a function at the time of invocation.
// It's particularly useful when you want to borrow functionality from another object, set the context for a function
/*
In JavaScript, the call, apply, and bind methods are used to call a function and set the "this" value inside that function. ( argument to be passed in this is optional )
call (thisValue, arg1, arg2, ... ) : This method calls the function and sets the "this" value for the function.
apply (thisValue, argArray): It is similar to call(), but it accepts arguments as an array. It executes a function, setting the `this` value and accepting arguments as an array.
bind(thisValue, arg1, arg2, ...) It is a method that creates a new function, binding the provided object as the this context. To consume this we need to call it later.
*/
function Person(fname, lname) {
this.firstName = fname;
this.lastName = lname;
}
Person.prototype.getFullName = function () {
return this.firstName + " " + this.lastName;
}
person1 = {
firstName: "Rajeev",
lastName: "Modi"
}
const p1 = new Person("Bittu", "Kumar");
console.log(p1.getFullName.call(person1))
const p2 = p1.getFullName.bind(person1)
console.log(p2());
console.log(p1.getFullName());
// understand with this example
function Car(company, fuelType) {
this.company = company;
this.fuelType = fuelType;
this.cardetails = function (color, price) {
return this.company + " " + this.fuelType + " " + color + " " + price;
}
}
const car1 = new Car("maruti", "petrol")
const car2 = { company: "hyundai", fuelType: "diesel" }
console.log("car details using call", car1.cardetails.call(car2, "red", "5000"));
console.log("car details using apply", car1.cardetails.apply(car2, ["blue", "6000"]));
const car3Bind= car1.cardetails.bind(car2, "yellow", "7000");
console.log("car details using bind",car3Bind());
// car details using call hyundai diesel red 5000
// car details using apply hyundai diesel blue 6000
// car3 bind hyundai diesel yellow 7000