|
Last change
on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
805 bytes
|
| Line | |
|---|
| 1 | function bind(func, thisObj, ...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 === bind.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 func(...args);
|
|---|
| 19 | }
|
|---|
| 20 | return func.apply(thisObj, args);
|
|---|
| 21 | };
|
|---|
| 22 | return bound;
|
|---|
| 23 | }
|
|---|
| 24 | const bindPlaceholder = Symbol('bind.placeholder');
|
|---|
| 25 | bind.placeholder = bindPlaceholder;
|
|---|
| 26 |
|
|---|
| 27 | export { bind };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.