0

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);
            }
        };
    });
2
  • 1
    The func() method you return takes its own input1 and input2 which shadow the input1 and input2 from the parent function. If you don't need parameters for the methods, just remove them and the func function will use the outer ones. Commented Jun 29, 2021 at 11:39
  • 1
    The input1 in your func : function is not the same as outside it. It is a local param. You can omit input1,and input2 from the definition. Commented Jun 29, 2021 at 11:39

2 Answers 2

1

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);
        }
    };
});
Sign up to request clarification or add additional context in comments.

Comments

1

The input1 and input2 in func : function are not the same as outside it. They are local params which will be passed on function call. You can omit input1 and input2 from the definition and call the function like this func().

console.log(input1, input2)
     //"input1", "input2"
        return {
            func: () => {        
               func(input1, input2);
            }
        };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.