Is there a way to pass part of inputs into function? [duplicate] - javascript

This question already has answers here:
Is there a way to provide named parameters in a function call in JavaScript?
(12 answers)
Closed 5 years ago.
Suppose I have a function as this:
function test(a, b, c, d) {
...
}
Is there a way that I can pass my inputs into the function as follows:
test(a:1,c;2)

Named values doesn't support in a function, you have 2 options:
Pass undefined value to unused variable like test(1, undefined, 2, undefined)
Modified the parameters as an object like test(obj) and use obj.a, obj.b,... to pass the values

You can not as you exactly suggested, but you could have your function take an object with your arguments as fields.
Ex:
function test(myArguments) {
if (myArguments.a) {
// do something with myArguments.a
}
...
}
Call as:
test({a: 1, b: 2});

Related

How to name an anonymous function inside a javascript function based on passed parameters? [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 9 months ago.
I want to give the anonymous function's name that is inside my function based on the parameters passed. For example, if the passed parameter is "nature" then I want to create an anonymous function with that name.
Example code:
function hello(nature) {
window.nature /* I want this word "nature" to be taken from the parameter passed by the function */ = function () {
console.log('succes');
}
}
You can do it by passing a string as parameter.
Here how to assign a custom name and a custom function, if needed.
function hello(name, func) {
window[name] = func;
}
hello('azerty', () => console.log('hello'));
window.azerty() // output 'hello'
Here is an exemple where variable "nature" has value "fn" and parameter passed to "fn" is logged to the console.
function hello(method) {
window[method] = function (param) {
console.log(param);
};
}
hello('fn');
window.fn('success');

How to determine function have param or not [duplicate]

This question already has an answer here:
Get argument count (or even names) of javascript function object
(1 answer)
Closed 11 months ago.
How can i determine if function have param or not
Like
function Foo(bar){}
And
function Foo(){}
I tried to use Foo.arguments but always return null at both
How can i know when function have param or know param length?
You can access the number of parameters a function has via .length. For example,
function Foo(bar){}
console.log(Foo.length); // 1
and
function Foo(){}
console.log(Foo.length); // 0

How to destructure an object into parameter names [duplicate]

This question already has answers here:
Is there a way to provide named parameters in a function call in JavaScript?
(12 answers)
How to get function parameter names/values dynamically?
(34 answers)
Closed 4 years ago.
I have many functions defined elsewhere that look like this
export function foo(bar1, bar2, bar3, ...) {
// does stuff
}
In the module I am working on I am passed both the function above and an object containing the parameters like this:
// params = {
// bar1: bar1Value,
// bar2: bar2Value,
// bar3: bar3Value,
// ...
// }
function myFunc(foo, params) {
}
I need to call func with the params in the correct order. It is an object not an array so I can't rely on the property order, I need to match up the params to the signature of the function. Is this possible?
Edit to add: There are hundreds of functions that can be passed in, I am trying to avoid refactoring them all, but it seems that it is impossible to look up the parameter names directly. However I've found this which shows it can be done by breaking down the function as a string.
You can access the properties of an object (like "params") either like
params.bar1 or like params["bar1"] and you can get the names of the properties by using Object.keys(params), if thats what your asking for.
See also https://www.w3schools.com/js/js_object_properties.asp and https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Pass unknown number of parameters to JS function [duplicate]

This question already has answers here:
What is the difference between call and apply?
(23 answers)
Closed 7 years ago.
A pattern in some javascript libraries is to be able to pass any number of parameters to a function:
functiona(param1)
functiona(param1, param2, param3)
functiona(param1, param2)
I have an array of unknown length, and I'd like to pass all the array items as parameters to a function like functiona(). Is this possible? If so, what is the syntax for doing this?
What you want is probably Function.prototype.apply().
Usage:
var params = [param1, param2, param3];
functiona.apply(this, params);
As others noted, functiona declaration may use arguments, e.g.:
function functiona()
{
var param1 = this.arguments[0];
var param2 = this.arguments[1];
}
But it can use any number of normal parameters as well:
function foo(x, y)
{
console.log(x);
}
foo.apply(this, [10, 0, null]); // outputs 10
Use arguments:
The arguments object is an Array-like object corresponding to the
arguments passed to a function.
Yep, all parameters passed to a JavaScript function can be accessed using the arguments array within the function.
function foo () {
console.log(arguments[0]); // -> bar
console.log(arguments[1]); // -> baz
}
foo('bar', 'baz');

How to pass a function in another function with variadic arguments with javascript? [duplicate]

This question already has answers here:
Calling dynamic function with dynamic number of parameters [duplicate]
(10 answers)
Closed 7 years ago.
I have a pre-process function, here's an example function
function preprocess (msg, fct) {
alert(msg);
fct(???);
}
I need to execute the function fct in the preprocess function, but fct not always has the same number of parameters. I am not a pro in javascript but I think there are 2 ways to achieve that :
explicit call with an object
function preprocess (msg, fct, obj) { ... }
usage : preprocess ('hello', myfct, {firstparam: 'foo', secondparam: 'bar'});
using the argument inner property of the function
anyways i might have the theory, i am not able to code both the case above. Is it possible to achieve what I need using both the ways ? if yes, could you provide a minimum example of each to show me the way ?
You can pass the arguments at the end in variadic form, and use the arguments object to get what you need:
function preprocess (msg, fct /*, ...as*/) {
var as = [].slice.call(arguments, 2);
alert(msg);
fct.apply(this, as);
}
preprocess(msg, fct, a, b, c); // fct(a, b, c)

Categories

Resources