-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path2693-call-function-with-custom-context.js
35 lines (34 loc) · 1.26 KB
/
2693-call-function-with-custom-context.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
/**
* 2693. Call Function with Custom Context
* https://leetcode.com/problems/call-function-with-custom-context/
* Difficulty: Medium
*
* Enhance all functions to have the callPolyfill method. The method accepts an object obj as
* its first parameter and any number of additional arguments. The obj becomes the this context
* for the function. The additional arguments are passed to the function (that the callPolyfill
* method belongs on).
*
* For example if you had the function:
*
* function tax(price, taxRate) {
* const totalCost = price * (1 + taxRate);
* console.log(`The cost of ${this.item} is ${totalCost}`);
* }
*
* Calling this function like tax(10, 0.1) will log "The cost of undefined is 11". This is
* because the this context was not defined.
*
* However, calling the function like tax.callPolyfill({item: "salad"}, 10, 0.1) will log "The
* cost of salad is 11". The this context was appropriately set, and the function logged an
* appropriate output.
*
* Please solve this without using the built-in Function.call method.
*/
/**
* @param {Object} context
* @param {Array} args
* @return {null|boolean|number|string|Array|Object}
*/
Function.prototype.callPolyfill = function(context, ...args) {
return this.bind(context)(...args);
}