-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_apply_bind.js
More file actions
55 lines (49 loc) · 2.12 KB
/
call_apply_bind.js
File metadata and controls
55 lines (49 loc) · 2.12 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
/**
* In JavaScript all functions are object methods. If a function is not a method of a JavaScript object, it is a function of the global object.
*/
// "use strict";
// call:
// With call(), an object can use a method belonging to another object.
const person = {
fullName: function() {
return this.firstName + " " + this.lastName;
},
identity: function(city, country){
return this.firstName + " " + this.lastName + " " + city + " " + country;
}
};
const person1 = {
firstName:"John",
lastName: "Doe"
};
console.log(person.fullName.call(person1));
console.log(person.identity.call(person1)); // city and country will be undefied
console.log(person.identity.call(person1, "Dhaka", "BD"));
// apply:
console.log(person.fullName.apply(person1));
// The apply() method is very handy if you want to use an array instead of an argument list.
// console.log(person.identity.apply(person1, "Dhaka", "BD")); // TypeError: CreateListFromArrayLike called on non-object
console.log(person.identity.apply(person1, ["Dhaka", "BD"]));
// console.log(person.identity.apply(person1, ["Dhaka", "BD", "ok"])); // 3rd value will be ignored. only 1st 2 value will be used.
// console.log(person.identity.apply(["a","b", "Dhaka", "BD"])); // all 4 value will be undefined, because we pass array for 1st argument, but it should be object. if we use strict mode then must pass something for 1st argument.
// console.log(person.identity.apply(null, ["Dhaka", "BD"])); // first 2 value will be undefined. if we use strict mode then must pass something for 1st argument.
// bind:
const car = {
carName: "Toyota",
showCarName: function () {
console.log("Car name is:", this.carName);
}
};
const superCar = {
// superCarName: "Lalalulu", // property name must be same for bind.
carName: "Lalalulu",
}
// car.showCarName();
const showCarName = car.showCarName;
showCarName(); // car name is undefined
// bind method hold the context(this) of object.
const showCarNameBind = car.showCarName.bind(car);
showCarNameBind();
// bind method borrow method from another object
const showSuperCarBind = car.showCarName.bind(superCar);
showSuperCarBind();