-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyfill_bind.js
37 lines (27 loc) · 1010 Bytes
/
polyfill_bind.js
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
let name = {
firstName: "Pankaj",
lastName: "Sha",
};
function printName(home, home1, home2) {
console.log(this.firstName + " " + this.lastName + " " + home + " " + home1 + " " + home2);
}
// let print1 = printName.bind(name, "delhi", "kol");
// print1("mumbai");
// polyfill for bind
// Note -> bind method is avaliable for all method (To implement this attached your bind method with function prototype)
// Borrowed function is attached with this keyword.
//
Function.prototype.myBind = function(...args){
// this holds reference of borrowed method
let obj = this;
// first argument refers the object for mybind method
// for other arguments use slice to get arr of other arguments
let params = args.slice(1);
return function (...args2) {
// when bind, it returns a method which invokes the borrowed method
obj.apply(args[0], [...params, ...args2])
};
};
let print = printName.myBind(name, "Delhi", "Kol");
console.log(print)
print("Mumbai")