Is there any way to get only the unnamed arguments?

Emil Lundberg

In JavaScript functions, arguments is an array-like object containing all arguments to the function, whether they are named or not:

function f(foo, bar) {
    console.log(arguments);
}
f(1, '2', 'foo'); // [1, "2", "foo"]

Is there a way to get only the arguments that are not named, so you could do something like this?

function f(foo, bar) {
    console.log('foo:', foo, 'bar:', bar, 'the rest:', unnamedArguments);
}
f(1, '2', 'foo'); // foo: 1 bar: "2" the rest: ["foo"]

But why?

A real-world use case is for injecting Angular modules as arguments into RequireJS modules:

define([
    'angular',
    'myLibModule', // Exports an Angular module object
    'myOtherLibModule', // Exports an Angular module object
], function(angular, myLibModule, myOtherLibModule) {
    angular.module('MyApp', [myLibModule.name, myOtherLibModule.name]);
});

As the list of module dependencies can get quite large, this quickly becomes very cumbersome. While I could solve it as

define([
    'angular',
    'underscore',
    'myLibModule', // Exports an Angular module object
    'myOtherLibModule', // Exports an Angular module object
], function(angular, _) {
    function angularModuleNames(modules) {
        return _.pluck(_.pick(modules, function(item) {
            return _.has(item, 'name');
        }), 'name');
    }
    angular.module('MyApp', angularModuleNames(arguments));
});

this is also rather cumbersome, and it would be nice if I could do something like this instead:

define([
    'angular',
    'underscore',
    'myLibModule', // Exports an Angular module object
    'myOtherLibModule', // Exports an Angular module object
], function(angular, _) {
    angular.module('MyApp', _.pluck(unnamedArguments, 'name'));
});

Of course, a way to group dependencies in RequireJS would suffice just as well for this particular use case.

Denys Séguret

The number of declared arguments is provided in the length property of the function.

So you can get the arguments whose index is greater or equal to this length :

var undeclaredArgs = [].slice.call(arguments, arguments.callee.length);

As you can't use arguments.callee in strict mode starting from ES5, you should use a reference to the function whenever possible :

var undeclaredArgs = [].slice.call(arguments, f.length);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Flutter: Is there any way of using a widget only on ios but not on android?

분류에서Dev

Is there any way to make the Entire Screen Read Only in wpf

분류에서Dev

Is there any way to get a MySQL database from a /var backup?

분류에서Dev

is there any easier way to get the LI list number in one UL?

분류에서Dev

Is there any way to get actual type from a string value?

분류에서Dev

I developed an app only on IPhone now i want the same app universal, is there any way to transform it on Xcode?

분류에서Dev

Is there any way to quickly and efficiently get a count of IP host addresses in thousands of CIDR/netmask ranges in Bash?

분류에서Dev

Any way to get Intel Graphics 3000 to run properly in Windows 10 version 1803?

분류에서Dev

How do I get only the PID, without any extra information, of a process running on port 3000?

분류에서Dev

JavaScript: get to arguments object of callback

분류에서Dev

Is there any way to kill a zombie process without reboot?

분류에서Dev

Is there any way to find non-awaited Tasks?

분류에서Dev

Is there any way to optimize c++ string += operator?

분류에서Dev

Way to do "Any" on an aggregation in SQL Server

분류에서Dev

Is there any way to localize marshmallow permissions strings in android?

분류에서Dev

Is there any way I could overlap another css?

분류에서Dev

Any way to cause a refresh of selectedItemReminder in JComboBox

분류에서Dev

Partition suddenly became empty, any way to restore it?

분류에서Dev

Is there any way to set a greeting message in ubuntu?

분류에서Dev

Is there any way to execute commands from history?

분류에서Dev

Is there any easy way to create oracle partitions

분류에서Dev

Is there any way of setting the buttons attribute of MouseEvent in Mozilla?

분류에서Dev

Is there any way to set rotation to the layout parameter

분류에서Dev

Any way to use multiple hosts files on windows?

분류에서Dev

Any way to end all processes on a particular drive?

분류에서Dev

Is there any way to have boxed and by-move closures?

분류에서Dev

Is there any way to create an action counter for a script? (Python)

분류에서Dev

Is there any way to invoke an external URI scheme?

분류에서Dev

Is there any way to display time in UTC on the panel?

Related 관련 기사

  1. 1

    Flutter: Is there any way of using a widget only on ios but not on android?

  2. 2

    Is there any way to make the Entire Screen Read Only in wpf

  3. 3

    Is there any way to get a MySQL database from a /var backup?

  4. 4

    is there any easier way to get the LI list number in one UL?

  5. 5

    Is there any way to get actual type from a string value?

  6. 6

    I developed an app only on IPhone now i want the same app universal, is there any way to transform it on Xcode?

  7. 7

    Is there any way to quickly and efficiently get a count of IP host addresses in thousands of CIDR/netmask ranges in Bash?

  8. 8

    Any way to get Intel Graphics 3000 to run properly in Windows 10 version 1803?

  9. 9

    How do I get only the PID, without any extra information, of a process running on port 3000?

  10. 10

    JavaScript: get to arguments object of callback

  11. 11

    Is there any way to kill a zombie process without reboot?

  12. 12

    Is there any way to find non-awaited Tasks?

  13. 13

    Is there any way to optimize c++ string += operator?

  14. 14

    Way to do "Any" on an aggregation in SQL Server

  15. 15

    Is there any way to localize marshmallow permissions strings in android?

  16. 16

    Is there any way I could overlap another css?

  17. 17

    Any way to cause a refresh of selectedItemReminder in JComboBox

  18. 18

    Partition suddenly became empty, any way to restore it?

  19. 19

    Is there any way to set a greeting message in ubuntu?

  20. 20

    Is there any way to execute commands from history?

  21. 21

    Is there any easy way to create oracle partitions

  22. 22

    Is there any way of setting the buttons attribute of MouseEvent in Mozilla?

  23. 23

    Is there any way to set rotation to the layout parameter

  24. 24

    Any way to use multiple hosts files on windows?

  25. 25

    Any way to end all processes on a particular drive?

  26. 26

    Is there any way to have boxed and by-move closures?

  27. 27

    Is there any way to create an action counter for a script? (Python)

  28. 28

    Is there any way to invoke an external URI scheme?

  29. 29

    Is there any way to display time in UTC on the panel?

뜨겁다태그

보관