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
Jake BackuesJake Backues 

difference between calling this and using the helper variable

In my helper js class, what is the difference between calling methods using the passed in helper from my parameters or calling a method using this? Is one better than the other? does one kick off a new asynchronous process and one stays synchronous with the current process?
 
({ 
    doInit : function (component, event, helper) {
        helper.getContacts(component, event);
        this.getContacts(component, event);
    },
    
    getContacts: function(component, event) {

    }
})

 
Best Answer chosen by Jake Backues
Jake BackuesJake Backues
@Hemant_Soni
I used the same code above.

I've talked with some Developer Group leaders and found out the answer to my question.

Turns out "this" and "helper" are exactly the same in the helper class. So there really is no need to pass the helper variable to your helper methods.

If you have a callback, you can declare a new variable as this and use that in the callback.

var helper = this;
action.setCallback(this, function(response){ helper.getContacts(); });

All Answers

Hemant_SoniHemant_Soni
Hi Jake Backues,
Here are few things which you know.
  • When you called like this.getContacts(component, event); then it call method of component controller.
  • When you called like helper.getContacts(component, event); then it call method of helper.
  • If we need to fetch data from sever side then we need to call helper.getContacts().
  • If we need to get data from client side then we are calling this.getContacts().
I guess it will help you understand difference.Please mark it solved if it helps you.
Thanks
Hemant
Jake BackuesJake Backues
@Hemant_Soni

There is no way this.getContacts is calling the method of the controller. I don't have a method called that in the javascript controller or the apex controller. "this" definitely calls helper methods.
Hemant_SoniHemant_Soni
Hi jake,
Actually "this" calls current class method if its in component's controller or it is in helper.So if you want to call method of current class then you can use this.
Thanks
Hemant
Jake BackuesJake Backues
@Hemant_Soni,

I tested that in a controller.js and it did not work.
Hemant_SoniHemant_Soni
Hi Jake,
Can you please share your code and explain what you are looking so i can help you better.
Thanks
Hemant
Jake BackuesJake Backues
@Hemant_Soni
I used the same code above.

I've talked with some Developer Group leaders and found out the answer to my question.

Turns out "this" and "helper" are exactly the same in the helper class. So there really is no need to pass the helper variable to your helper methods.

If you have a callback, you can declare a new variable as this and use that in the callback.

var helper = this;
action.setCallback(this, function(response){ helper.getContacts(); });
This was selected as the best answer