|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
837 bytes
|
| Rev | Line | |
|---|
| [a762898] | 1 | function bindKey(object, key, ...partialArgs) {
|
|---|
| 2 | const bound = function (...providedArgs) {
|
|---|
| 3 | const args = [];
|
|---|
| 4 | let startIndex = 0;
|
|---|
| 5 | for (let i = 0; i < partialArgs.length; i++) {
|
|---|
| 6 | const arg = partialArgs[i];
|
|---|
| 7 | if (arg === bindKey.placeholder) {
|
|---|
| 8 | args.push(providedArgs[startIndex++]);
|
|---|
| 9 | }
|
|---|
| 10 | else {
|
|---|
| 11 | args.push(arg);
|
|---|
| 12 | }
|
|---|
| 13 | }
|
|---|
| 14 | for (let i = startIndex; i < providedArgs.length; i++) {
|
|---|
| 15 | args.push(providedArgs[i]);
|
|---|
| 16 | }
|
|---|
| 17 | if (this instanceof bound) {
|
|---|
| 18 | return new object[key](...args);
|
|---|
| 19 | }
|
|---|
| 20 | return object[key].apply(object, args);
|
|---|
| 21 | };
|
|---|
| 22 | return bound;
|
|---|
| 23 | }
|
|---|
| 24 | const bindKeyPlaceholder = Symbol('bindKey.placeholder');
|
|---|
| 25 | bindKey.placeholder = bindKeyPlaceholder;
|
|---|
| 26 |
|
|---|
| 27 | export { bindKey };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.