function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Soundhariyaa MSoundhariyaa M 

Call from Controller to Helper

Hi,

Controller functions:

A:function(component, event, helper){
   helper.getExample(component,event);
},
B:function(component, event, helper){
   helper.getExample(component,event,parameters);
},
C:function (component, event, helper){
   helper.getExample(component,event)
},
D:function (component, event, helper)//which is actually a search function
{
    helper.getExample(component,event,searchString);
},
E:function(component, event, helper){
   helper.getExample(component,event,searchString,numberofrecords);
}

Helper function:

getExample:function(component,parameters,searchString,numberofrecords)
{
},

My doubt is in A (Controller function),we are passing event but in helper function we don't have event parameter in function declaration.
In D (Controller function),searchString is passed to helper as third argument and it is getting assigned correctly to  searchString without getting assigned to parameters.How???

In C (Controller function), event is passed but we don't have event in helper function.

And the code is working fine without any errors?!

How is this even possible?
I guess I'm lacking some concept.Could you help me understand this?!
 
Best Answer chosen by Soundhariyaa M
vishal-negandhivishal-negandhi

Hello Soundhariyaa, 

 

Let's look at function A
 

// controller
A:function(component, event, helper){
   helper.getExample(component,event);
},

// helper
getExample:function(component,parameters,searchString,numberofrecords)
{
},

Here, when your helper method is called

event is assigned to parameters and you don't need parameters name to match. First controller parameter will be assigned to the first helper parameter, second to second and so on. 

And that's why if you debug the last 2 parameters here, you will get "undefined" because you're only passing 2 parameters. 
In javascript, unlike in Apex, we don't specify the function parameter type, just the names. 

So when a javascript method is invoked, the parameters will be assigned values in order of the values passed to them. 

That's the reason why
- IN A, event is assigned to parameters. It's the second parameter in the function and you're passing "event" as the second parameter when you call the helper method. 

- in D, searchString is passed as the third parameter, and that is why it is assigned to searchString in helper, being the third parameter. 

I hope this clarifies and helps you understand.

 

Best,

Vishal