Skip to content
Advertisement

Pass input parameters to returned JS functions

I am trying to pass input parameters from one function to another function inside a return statement. However in below example input1 and input2 are undefined. The values inside the return statement are undefined while inside the factory function they are not. How do I pass the values into the returned func()?

   function func(input1,input2) {
     console.log(input1,input2)
     // "undefined, undefined"
   }
    
    angular.module("factory").factory("test", (input1, input2) => {
     console.log(input1, input2)
     //"input1", "input2"
        return {
            func: (input1, input2) => {
                func(input1, input2);
            }
        };
    });

Advertisement

Answer

This line:

func: (input1, input2) => {

shadows the parameter of the outer function (by declaring it’s own parameters with the same names). So just remove those. E.g.

angular.module("factory").factory("test", (input1, input2) => {
    return {
        func: () => {
            func(input1, input2);
        }
    };
});
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement