-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path2754-bind-function-to-context.js
More file actions
38 lines (37 loc) · 1.03 KB
/
2754-bind-function-to-context.js
File metadata and controls
38 lines (37 loc) · 1.03 KB
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
/**
* 2754. Bind Function to Context
* https://leetcode.com/problems/bind-function-to-context/
* Difficulty: Medium
*
* Enhance all functions to have the bindPolyfill method. When bindPolyfill is called with
* a passed object obj, that object becomes the this context for the function.
*
* For example, if you had the code:
* function f() {
* console.log('My context is ' + this.ctx);
* }
* f();
*
* The output would be "My context is undefined". However, if you bound the function:
* function f() {
* console.log('My context is ' + this.ctx);
* }
* const boundFunc = f.boundPolyfill({ "ctx": "My Object" })
* boundFunc();
*
* The output should be "My context is My Object".
*
* You may assume that a single non-null object will be passed to the bindPolyfill method.
*
* Please solve it without the built-in Function.bind method.
*/
/**
* @param {Object} obj
* @return {Function}
*/
Function.prototype.bindPolyfill = function(obj) {
var fn = this;
return function() {
return fn.apply(obj, arguments);
};
};