forked from pluralsight/web-dev-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyfill.js
69 lines (61 loc) · 1.47 KB
/
polyfill.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
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
63
64
65
66
67
68
69
const me = {
firstName: "Srimal",
lastName: "Priyanga",
};
const imal = {
firstName: "Imalka",
lastName: "Perera",
};
//1
function printName1(person) {
return console.log(`1 My name is ${person.firstName} ${person.lastName}`);
}
printName1(me);
//2
function printName2(method) {
return console.log(
`2.${method} My name is ${this.firstName} ${this.lastName}`
);
}
printName2.call(me, "call");
printName2.apply(me, ["apply"]);
let myName = printName2.bind(me, "bind");
myName();
printName2.call(imal, "call");
printName2.apply(imal, ["apply"]);
let imalName = printName2.bind(imal, "bind");
imalName();
//3
function printName3(method) {
return console.log(
`3.${method} My name is ${this.firstName} ${this.lastName}`
);
}
Function.prototype.myBind = function (...params) {
let fun = this,
params2 = params.slice(1);
return function () {
fun.call(params[0], params2);
};
};
let polyFillMyBind = printName3.myBind(me, "callingToMyBind");
console.log(polyFillMyBind);
polyFillMyBind();
//4
function printName4(method) {
return console.log(
`4.${method} My name is ${this.firstName} ${this.lastName}`
);
}
Function.prototype.myCall = function (context, ...args) {
context.myFn = this;
context.myFn(...args);
};
Function.prototype.myCall2 = function (...params) {
let self = params[0];
self.fun = this;
params2 = params.slice(1);
self.fun([params2]);
};
printName4.myCall(me, "callingToMyCall");
printName4.myCall2(imal, "callingToMyCall2");