-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2754-BindFunctionToContext.js
More file actions
116 lines (101 loc) · 3.09 KB
/
2754-BindFunctionToContext.js
File metadata and controls
116 lines (101 loc) · 3.09 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 2754. Bind Function to Context
// 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.
// Example 1:
// Input:
// fn = function f(multiplier) {
// return this.x * multiplier;
// }
// obj = {"x": 10}
// inputs = [5]
// Output: 50
// Explanation:
// const boundFunc = f.bindPolyfill({"x": 10});
// boundFunc(5); // 50
// A multiplier of 5 is passed as a parameter.
// The context is set to {"x": 10}.
// Multiplying those two numbers yields 50.
// Example 2:
// Input:
// fn = function speak() {
// return "My name is " + this.name;
// }
// obj = {"name": "Kathy"}
// inputs = []
// Output: "My name is Kathy"
// Explanation:
// const boundFunc = f.bindPolyfill({"name": "Kathy"});
// boundFunc(); // "My name is Kathy"
// Constraints:
// obj is a non-null object
// 0 <= inputs.length <= 100
/**
* @param {Object} obj
* @return {Function}
*/
Function.prototype.bindPolyfill = function(obj) {
var that = this;
return function(...args) {
// use apply
return that.apply(obj, args)
}
}
Function.prototype.bindPolyfill1 = function(obj) {
var that = this;
return function(...args) {
// 使用 bind,IE9 才开始支持。
return that.bind(obj, ...args)()
}
}
Function.prototype.bindPolyfill2 = function(obj) {
var that = this;
return function(...args) {
// 使用 不能使用的 call
return that.call(obj, ...args)
}
}
// use Symbol
Function.prototype.bindPolyfill3 = function(obj) {
const sb = Symbol('fn');
obj[sb] = this;
return (...args) => {
return obj[sb](...args)
}
}
function f(multiplier) {
return this.x * multiplier;
}
let boundFunc = f.bindPolyfill({"x": 10});
console.log(boundFunc(5)); // 50
boundFunc = f.bindPolyfill1({"x": 10});
console.log(boundFunc(5)); // 50
boundFunc = f.bindPolyfill2({"x": 10});
console.log(boundFunc(5)); // 50
boundFunc = f.bindPolyfill3({"x": 10});
console.log(boundFunc(5)); // 50
function speak() {
return "My name is " + this.name;
}
obj = {"name": "Kathy"}
boundFunc = speak.bindPolyfill({"name": "Kathy"});
console.log(boundFunc()); // "My name is Kathy"
boundFunc = speak.bindPolyfill1({"name": "Kathy"});
console.log(boundFunc()); // "My name is Kathy"
boundFunc = speak.bindPolyfill2({"name": "Kathy"});
console.log(boundFunc()); // "My name is Kathy"
boundFunc = speak.bindPolyfill3({"name": "Kathy"});
console.log(boundFunc()); // "My name is Kathy"