Pass Arguments to Function

Passing a function with parameters as a parameter?

Use a "closure":

$(edit_link).click(function(){ return changeViewMode(myvar); });

This creates an anonymous temporary function wrapper that knows about the parameter and passes it to the actual callback implementation.

How can I pass an argument in a function and define that argument as a variable when I call the function?

const starts a variable declaration. Variable declaration are statements so they cannot be part of the arguments list of a call expression.

Unclear why you get that error in the second case. While b is a parameter, that doesn't prevent you from declaring another variable with the same name (especially in a different scope).

Either way, you don't need that second parameter. It doesn't serve any purpose. You can't declare "dynamic" variables that way and there is no reason to do so anyway. It doesn't matter what the name of the variable is.

Just do:

function loadSteps(title, id){
if(title != ""){
const element = document.getElementById(id);
if (!element) {
console.error(`No such element: ${id}`);
return;
}
element.click();
}
}

loadSteps(stepOneTitle, "button-step-one" )
loadSteps(stepTwoTitle, "button-step-two" )

Passing parameters to a Bash function

There are two typical ways of declaring a function. I prefer the second approach.

function function_name {
command...
}

or

function_name () {
command...
}

To call a function with arguments:

function_name "$arg1" "$arg2"

The function refers to passed arguments by their position (not by name), that is $1, $2, and so forth. $0 is the name of the script itself.

Example:

function_name () {
echo "Parameter #1 is $1"
}

Also, you need to call your function after it is declared.

#!/usr/bin/env sh

foo 1 # this will fail because foo has not been declared yet.

foo() {
echo "Parameter #1 is $1"
}

foo 2 # this will work.

Output:

./myScript.sh: line 2: foo: command not found
Parameter #1 is 2

Reference: Advanced Bash-Scripting Guide.

Cannot pass arguments in the invoke function I recreated

In both the array and the object case, you show a call that doesn't attempt to pass args to the method:

newArr.push(element[methodName]());

Since args is an array, the easiest way to pass them is by using apply. apply takes two arguments. The first is whatever should be considered this inside the call to the method, which is element in this case. The second argument is the array of arguments that should be passed to the method. We end up with this form:

newArr.push(element[methodName].apply(element, args));

Having answered the core of your question, let's see how else we could make your version of invoke better. First, we will take a look at the array case:

    for (let index = 0; index < collection.length; index++) {

let keysArr = Object.keys(collection);
let element = collection[keysArr[index]];

newArr.push(element[methodName].apply(element, args));
};

The way in which you determine element here is a bit inefficient. You recompute Object.keys(collection) on every iteration, even though it never changes. Moreover, you do not actually need keysArr; element is just collection[index]. So we can change the array part into this:

    for (let index = 0; index < collection.length; index++) {
let element = collection[index];
newArr.push(element[methodName].apply(element, args));
};

We have a similar problem in the object part:

    for (let index = 0; index < Object.entries(collection).length; index++) {

let keysArr = Object.keys(collection);
let element = collection[keysArr[index]];

newArr.push(element[methodName].apply(element, args));
}

Besides Object.keys(collection), you are also recomputing Object.entries(collection) on every iteration, which also never changes. However, in this case, you do need keysArr. The solution is to compute it once before the loop and reuse it:

    let keysArr = Object.keys(collection);
for (let index = 0; index < keysArr.length; index++) {
let element = collection[keysArr[index]];
newArr.push(element[methodName].apply(element, args));
}

At this point, we have an efficient implementation of _.invoke that works. However, since this is Underscore, let us also try whether we can introduce more functional style.

Functional style is all about how we can compose existing functions into new functions. In the specific case of _.invoke, we can see that it is essentially a special case of _.map. Since _.map already knows how to iterate over arrays as well as objects and it already returns a new array, just like _.invoke, this means that we can reduce our challenge. Instead of doing the "call a method with arguments" thing for the whole collection, we just need to figure out how to do it for a single element, and then compose that somehow with _.map.

Starting with just a function that does the job for a single element, we already know that it goes like this:

function invokeElement(element, methodName, args) {
return element[methodName].apply(element, args);
}

Now, this version of invokeElement is not quite ready to be passed to _.map. _.map will know which element to pass, but it knows nothing about methodName or args. Somehow, we need to pass methodName and args "ahead of time", so that _.map will be able to suffice just by passing element. _.partial lets us do exactly that:

const partialInvoke = _.partial(invokeElement, _, methodName, args);

This line means: take invokeElement and create a new version of the function, where the second and third argument are already set to methodName and args, respectively, but the first argument still depends on what will be passed in the future. The underscore _ used here as a placeholder is the very same underscore as in _.map and _.partial, i.e., the default export of the Underscore library.

Now, we have everything we need to compose invoke out of _.map and invokeElement:

function invoke(collection, methodName) {
const args = Array.prototype.slice.call(arguments, 2);
const partialInvoke = _.partial(invokeElement, _, methodName, args);
return _.map(collection, partialInvoke);
}

We can still do better. Using _.restArguments, we no longer need to compute args:

const invoke = _.restArguments(function(collection, methodName, args) {
const partialInvoke = _.partial(invokeElement, _, methodName, args);
return _.map(collection, partialInvoke);
});

Alternatively, we can use modern spread syntax, which didn't exist yet when _.restArguments was invented:

function invoke(collection, methodName, ...args) {
const partialInvoke = _.partial(invokeElement, _, methodName, args);
return _.map(collection, partialInvoke);
}

Regardless, we have our own implementation of invoke in just two lines of code. That's the power of functional style!

How to pass arguments to a function in flutter?

Try widget.fromDate and widget.toDate. Coz I'm seeing you're using a stateful widget.

If your function takes positional arguments, eg
Future doSomething(String name, Bool jerk) {
Lots of code
}

Then when calling it inside a stateful widget, while using some arguments you passed to that widget, do tgis
blah blah = doSomething(widget.argument1, widget.argument2)

If it's not positional arguments, eg
Future doSomething({String name, Bool jerk} ) {
Lots of code
}

, then it's gonna be
blah blah = doSomething(
name: widget.argument1, jerk: widget.argument2)

How to pass arguments to functions in another file? - React 17.0.1

Problem is you are destructuring a string which doesn't have a property called someArg. That's why undefined is displayed.

Correct Way:

const Foo= (someArg) => { // No destructuring
alert(someArg);
};

export default Foo;

function App() {
useEffect(() => {
Foo("text"); // You are passing a string
}, []);
const Foo= ({ someArg }) => { // Doing destructuring
alert(someArg);
};

export default Foo;

function App() {
useEffect(() => {
Foo({someArg: "text"}); // You are passing an object
}, []);

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment



Related Topics



Leave a reply



Submit