invoking method with undefined parameter without getting an errorAsk Questions

 Posted on 14/07/2018 8:08:24 AM

I created a method that will call a particular method along it's parameters from an array.

function Invoker(checking, methodName, methodParams) {
   var counter = 0;
    angular.forEach(checking, function (data){
          if (angular.isDefined(data)){
              counter += 1;
          }
    });
    if (counter === checking.length) {
       methodName.apply(this, methodParams);   
    }
}

The 1st parameter is an array of variables that would be checked if its undefined or not. If not, the counter will be incremented.

2nd parameter is simply the method name that would be invoked.

3rd parameter is an array containing the parameters of the method being invoked.

Invoker([structureOne.groupB, structureTwo.groupC],
        justAMethod,
        [structureOne.groupB.name, structureTwo.groupB.code,
         structureTwo.groupC.id])

My problem is, when i call the Invoker function it crashes with undefined variable (is not defined error). I dont want to use an if statement before calling Invoker as this will defeat the purpose of the method.

The primary purpose of making the method is to lessen the sonarlint complexity of the js file caused by lots of angular.isDefined checks. The method greatly reduces the complexity but crashes when undefined variable is put to the method calling.

Is there a way I can call Invoker method without breaking the code and not putting nested if statements/angular.isDefined checks, try catches, before the call?

Share On: facebook gplus twitter
profile
Asked by ketan Linbachiya on 14/07/2018 8:08:24 AM Score: 269 points
Add Comment:
Submit

Comments

0 Answer

0 Corrected Answers

Post Your Answer

  •  
  •  

Existing Members

Sign in to your account
Email Address
Password
...or Join us
Download, Vote, Comment, Publish.
Full Name
Email Address
I have read and agree to the Terms of Service and Privacy Policy
Please subscribe me to the StoodQ newsletters
Guideline to answer a question:

Useful tips to submit your answer
Please read below guidelines before you submit your answer for question.

  • Read and understand question for which you are submitting your answer.
  • Try to avoid grammatical and spell mistake while answering.
  • Do not post any irrelevant information in your answer.
  • Explain your answer with example or any reference link to help who posted question.
  • If you find irrelevant question, please report it to support. Click here to contact support.
  • You agree to the privacy policy and terms of use to submit any contents.

Note: StoodQ is online developers community which helps developer for their difficulty, lets help them with your value contribution.