forked from pluralsight/web-dev-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyfill_mybind.js
35 lines (31 loc) · 886 Bytes
/
polyfill_mybind.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
35
let user = {
firstName: "Srimal",
lastName: "Priyanga",
};
function printFullName(age, surname, country) {
console.log(
`${this.firstName} ${this.lastName} ${surname}, ${age} , ${country}`
);
}
const myName = printFullName.bind(user, 31, "Fonseka");
myName("Sri Lanka");
/**
* myBind PolyFil
* @param {...any} params
* @returns
* @func access calling `function` by this
* @self get `this` as `self` to pass in myBind
* @args get other arguments
* @innerParams get invocation time arguments
* [...args, ...innerParams] pass combines arguments array to `apply`
*/
Function.prototype.myBind = function (...params) {
let func = this,
self = params[0],
args = params.slice(1);
return function (...innerParams) {
func.apply(self, [...args, ...innerParams]);
};
};
const myName2 = printFullName.myBind(user, 31, "Fonseka");
myName2("Sri Lanka");