Skip to content

Commit 8821efe

Browse files
committedSep 2, 2024
Add apply polyfill
1 parent 4a7d14a commit 8821efe

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed
 

‎src/javascript/algorithms/misc/polyfill/function/apply.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Function.prototype.myCall = function(thisArg, ...args) {
1+
Function.prototype.myApply = function(thisArg, args=[]) {
22
thisArg = thisArg || globalThis;
33

44
const uniqueId = Symbol('fn');
@@ -10,7 +10,7 @@ Function.prototype.myCall = function(thisArg, ...args) {
1010
return result;
1111
}
1212

13-
Function.prototype.myCall1 = function(thisArg, ...args) {
13+
Function.prototype.myApply1 = function(thisArg, args=[]) {
1414
const uniqueId = Symbol('fn');
1515
const wrappedObj = Object(thisArg);
1616
Object.defineProperty(wrappedObj, uniqueId, {
@@ -21,11 +21,11 @@ Function.prototype.myCall1 = function(thisArg, ...args) {
2121
return wrappedObj[uniqueId](...args);
2222
}
2323

24-
Function.prototype.myCall2 = function(thisArg, ...args) {
25-
return this.apply(thisArg, [...args]);
24+
Function.prototype.myApply2 = function(thisArg, args=[]) {
25+
return this.call(thisArg, ...args);
2626
}
2727

28-
Function.prototype.myCall3 = function(thisArg, ...args) {
28+
Function.prototype.myApply3 = function(thisArg, args=[]) {
2929
return this.bind(thisArg, ...args)();
3030
}
3131

@@ -38,7 +38,7 @@ function details(age = 30) {
3838
return this.firstName + ' '+this.lastName + ' is ' + age + ' years old';
3939
}
4040

41-
console.log(details.myCall(person1, 35));
42-
console.log(details.myCall1(person1, 35));
43-
console.log(details.myCall2(person1, 35));
44-
console.log(details.myCall3(person1, 35));
41+
console.log(details.myApply(person1, [35]));
42+
console.log(details.myApply1(person1, [35]));
43+
console.log(details.myApply2(person1, [35]));
44+
console.log(details.myApply3(person1, [35]));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Function.prototype.myCall = function(thisArg, ...args) {
2+
thisArg = thisArg || globalThis;
3+
4+
const uniqueId = Symbol('fn');
5+
thisArg[uniqueId] = this;
6+
7+
const result = thisArg[uniqueId](...args);
8+
delete thisArg[uniqueId];
9+
10+
return result;
11+
}
12+
13+
Function.prototype.myCall1 = function(thisArg, ...args) {
14+
const uniqueId = Symbol('fn');
15+
const wrappedObj = Object(thisArg);
16+
Object.defineProperty(wrappedObj, uniqueId, {
17+
enumerable: false,
18+
value: this
19+
});
20+
21+
return wrappedObj[uniqueId](...args);
22+
}
23+
24+
Function.prototype.myCall2 = function(thisArg, ...args) {
25+
return this.apply(thisArg, [...args]);
26+
}
27+
28+
Function.prototype.myCall3 = function(thisArg, ...args) {
29+
return this.bind(thisArg, ...args)();
30+
}
31+
32+
const person1 = {
33+
firstName: 'Sudheer',
34+
lastName: 'Jonna'
35+
}
36+
37+
function details(age = 30) {
38+
return this.firstName + ' '+this.lastName + ' is ' + age + ' years old';
39+
}
40+
41+
console.log(details.myCall(person1, 35));
42+
console.log(details.myCall1(person1, 35));
43+
console.log(details.myCall2(person1, 35));
44+
console.log(details.myCall3(person1, 35));

0 commit comments

Comments
 (0)
Please sign in to comment.