Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4d46deb

Browse files
author
Swastikyadav
committed
Update binding methods polyfills
1 parent 0432dc0 commit 4d46deb

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

‎Polyfills/polyApply.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@ printName.apply(obj, ["Delhi", "India"]);
1818
// Solution
1919
Function.prototype.polyApply = function(context, args) {
2020
// 'this' points to printName function
21-
context.fn = this;
21+
// context.fn = this;
2222

23-
return context.fn(...args);
23+
// return context.fn(...args);
24+
25+
const symbol = Symbol();
26+
context[symbol] = this;
27+
28+
const result = context[symbol](...args);
29+
delete context[symbol];
30+
31+
return result;
2432
}
2533

2634
// Using polyApply method.

‎Polyfills/polyBind.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,22 @@ nativePrintNameBind("Delhi", "India");
1919
// Solution
2020
Function.prototype.polyBind = function(context, ...args) {
2121
// 'this' points to printName function
22-
let callback = this;
22+
// let callback = this;
2323

24-
return function(...args2) {
25-
callback.apply(context, [...args, ...args2]);
26-
};
24+
// return function(...args2) {
25+
// callback.apply(context, [...args, ...args2]);
26+
// };
27+
28+
return (...args2) => {
29+
const symbol = Symbol();
30+
31+
context[symbol] = this;
32+
33+
const result = context[symbol](...args, ...args2);
34+
delete context[symbol];
35+
36+
return result;
37+
}
2738
}
2839

2940
// Using polyBind method.

‎Polyfills/polyCall.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@ printName.call(obj, "Delhi", "India");
1818
// Solution
1919
Function.prototype.polyCall = function(context, ...args) {
2020
// 'this' points to printName function
21-
context.fn = this;
21+
// context.fn = this;
2222

23-
return context.fn(...args);
23+
// return context.fn(...args);
24+
25+
const symbol = Symbol();
26+
context[symbol] = this;
27+
28+
const result = context[symbol](...args);
29+
delete context[symbol];
30+
31+
return result;
2432
}
2533

2634
// Using polyCall method.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /