Pass parameter to function without order

I have a function like this:

let showNotification = function(a,b,c,d,e,f){ console.log(a,b,c,d,e,f); } 

When calling this function, I know that I need to follow the order, something like this (if I do not want to pass a parameter to c, d, e):

 showNotification(1,2,,,,6); 

But this makes me make sure that I pass the entire parameter, and it should be fine.

I am looking for a better way to pass param in this case.

I know that I can create a param object instead instead of passing values. something like that:

 let showNotification = function(objectParam){ console.log(objectParam.a, objectParam.b, objectParam.c, objectParam.d, objectParam.e, objectParam.f) } 

then name it:

 showNotification({a: 1, b: 2, e:6}); 

I can pass the whole object, but that is not what I am looking for. I do not want to create an object for this every time.

Thinking out loud if there is a way to pass in string values ​​without worrying about order.

I see some dead SO posts regarding this, but no one has a solution.

+5
source share
3 answers

TL DR I know that you didn’t want to use an object, but modern JavaScript engines work very quickly on creating and deleting objects, as well as with default parameters and parameter destruction, I think the options object is your best option. For more details see "Notes on object transfer (default, destructuring)" below.


You have considered two main parameters (I will make a few notes about the version for transmission in the object below). The third option is to accept the builder template option, but it also involves using an object. You said you didn’t want to do this, but you didn’t say why not. Please note that modern JavaScript engines create and delete objects really, really fast, if this relates to your problem.

Marker Lines

When reading the fourth option, the fourth option appeared:> Bhushan Babar append / prepend idea : instead, you can use marker lines in the argument list to indicate that the next argument, for example:

 showNotification("the required parameter", "a", value_for_a, "c", value_for_c); 

At first glance, which is not related to creating an object, but on modern JavaScript engines, processing it will create an object that was not otherwise created: arguments , a pseudo-array of passed arguments. Because (or the rest parameter, which also creates the object) is the only reasonable way that you could consume such a thing.

Option on Builder

In this approach, the main function returns a builder with setters for various options, and then the final call "Yes, we are ready", which starts the process (where in the builder, the final call usually creates the final object). Using this would look something like this:

 showNotification("the required param") .withA(value_for_a) .withC(value_for_c) .go(); 

Implementing this is complicated compared to other approaches, but not complicated.

Object transfer notes (default, destructuring)

If you use an object (despite its unwillingness), you can use the default and destructuring parameters to make the object more convenient for working with:

 let showNotification = function({a = 1, b = 2, c = 3, d = 4, e = 5, f = 6} = {/*default if no object at all*/a: "foo"}){ console.log(a,b,c,d,e,f); }; showNotification(); showNotification({}); showNotification({a:42}); 

In the comment you said:

the thing here is all optional except the first parameter

It looks like you probably want the first parameter and then the options object:

 let showNotification = function(firstThing, {a = "default_a", b = "default_b"/*etc.*/} = {}) { console.log(firstThing, a, b); }; 
+1
source

@T,J. Crowder @T,J. Crowder answer better than that. Otherwise, you can do this using spread operators . As you said the thing is here all are optional, except the first param . Therefore, you must pass the value of the first parameter. then you do not need to pass another value if you do not want it. But you must specify the position of the parameter value as empty with , in the array. In my case you should create an object or array.

For example . If you have a dynamic array with values ​​(regardless of the position, which must be mandatory) and position [1,,,3] . then the distribution operator helps to combine this dynamic array with your function parameters.

 let showNotification = function(a,b,c,d,e,f){ console.log(a,b,c,d,e,f); }; let parts = [2,3,,,6];//Think it is a dynamic array on run time or compile time showNotification(1,...parts); 

And here @Felix Kling answers can help with Named Parameters

 var parameterfy = (function() { var pattern = /function[^(]*\(([^)]*)\)/; return function(func) { // fails horribly for parameterless functions ;) var args = func.toString().match(pattern)[1].split(/,\s*/); return function() { var named_params = arguments[arguments.length - 1]; if (typeof named_params === 'object') { var params = [].slice.call(arguments, 0, -1); if (params.length < args.length) { for (var i = params.length, l = args.length; i < l; i++) { params.push(named_params[args[i]]); } return func.apply(this, params); } } return func.apply(null, arguments); }; }; }()); var foo = parameterfy(function(a, b, c) { console.log('a is ' + a, ' | b is ' + b, ' | c is ' + c); }); foo(1, 2, 3); // a is 1 | b is 2 | c is 3 foo(1, {b:2, c:3}); // a is 1 | b is 2 | c is 3 foo(1, {c:3}); // a is 1 | b is undefined | c is 3 foo({a: 1, c:3}); // a is 1 | b is undefined | c is 3 

Other examples:

Pass the value to a specific parameter without worrying about the position of the parameter

Passing argument name when calling function in javascript

JavaScript: get the value of the argument and NAME of the passed variable

+1
source

Bhushan Babar did not send his proposal as an answer, so I will post it as a wiki community:

You can add or add (concat) tokens for the corresponding parameter to your lines, for example, if you want to send a parameter that represents the d parameter, and the line you want to pass is "myString", then you can choose the format of your token for example $ & d & $, so your parameter will look like this: "myString $ & d & $"

+1
source

Source: https://habr.com/ru/post/1275531/


All Articles