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 79f16c2

Browse files
author
Swastikyadav
committed
Added polyfills for call, apply, and bind methods
1 parent 1ff4319 commit 79f16c2

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

‎Polyfills/polyApply.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
- Write a polyfill for JavaScript's native apply method.
3+
*/
4+
5+
let obj = {
6+
firstName: "Swastik",
7+
lastName: "Yadav",
8+
// fn() {console.log(`I am ${this.firstName} ${this.lastName}`)} - This is to be achieved.
9+
}
10+
11+
function printName(city, country) {
12+
console.log(`I am ${this.firstName} ${this.lastName} from ${city} ${country}`);
13+
}
14+
15+
// Native apply method.
16+
printName.apply(obj, ["Delhi", "India"]);
17+
18+
// Solution
19+
Function.prototype.polyApply = function(context, args) {
20+
// 'this' points to printName function
21+
context.fn = this;
22+
23+
return context.fn(...args);
24+
}
25+
26+
// Using polyApply method.
27+
printName.polyApply(obj, ["Delhi", "India"]);

‎Polyfills/polyBind.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
- Write a polyfill for JavaScript's native bind method.
3+
*/
4+
5+
let obj = {
6+
firstName: "Swastik",
7+
lastName: "Yadav",
8+
// fn() {console.log(`I am ${this.firstName} ${this.lastName}`)} - This is to be achieved.
9+
}
10+
11+
function printName(city, country) {
12+
console.log(`I am ${this.firstName} ${this.lastName} from ${city} ${country}`);
13+
}
14+
15+
// Native bind method.
16+
const nativePrintNameBind = printName.bind(obj);
17+
nativePrintNameBind("Delhi", "India");
18+
19+
// Solution
20+
Function.prototype.polyBind = function(context, ...args) {
21+
// 'this' points to printName function
22+
let callback = this;
23+
24+
return function(...args2) {
25+
callback.apply(context, [...args, ...args2]);
26+
};
27+
}
28+
29+
// Using polyBind method.
30+
const polyPrintNameBind = printName.polyBind(obj);
31+
polyPrintNameBind("Delhi", "India");

‎Polyfills/polyCall.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
- Write a polyfill for JavaScript's native call method.
3+
*/
4+
5+
let obj = {
6+
firstName: "Swastik",
7+
lastName: "Yadav",
8+
// fn() {console.log(`I am ${this.firstName} ${this.lastName}`)} - This is to be achieved.
9+
}
10+
11+
function printName(city, country) {
12+
console.log(`I am ${this.firstName} ${this.lastName} from ${city} ${country}`);
13+
}
14+
15+
// Native call method.
16+
printName.call(obj, "Delhi", "India");
17+
18+
// Solution
19+
Function.prototype.polyCall = function(context, ...args) {
20+
// 'this' points to printName function
21+
context.fn = this;
22+
23+
return context.fn(...args);
24+
}
25+
26+
// Using polyCall method.
27+
printName.polyCall(obj, "Delhi", "India");

0 commit comments

Comments
(0)

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