+ Start a Discussion
Josh StigallJosh Stigall 

Understanding Function Parameters (For a Newbie)

Hi all. I am very new to JavaScript in general and Salesforce development in particular. I think that this question is definitely a newbie question, but I need to get over my hesitation to ask. I'm happy if the best answer is to point to learning resources that will help. The code below comes from the Expenses Component build in the Lightning Basics Trailhead. I know the code works, but I want to know why it works. (The code is in the expensesHelper.js.)
  1. What exactly is the significance of the 'component' and 'expense' parameters in the function? Does 'component' refer back to a component (in this case the Expenses component)? Does 'expense' refer to an attribute somewhere? (I don't think it would refer to the Expenses component becuase the attribute set there is named 'expenses.')
  2. In the second line: Are 'component' and 'expense' repeated because they are defined above?
  3. In the final line: How does the component.set("v.expenses", expenses); work? I know v.expenses refers to the Expenses component, but where does the value (I think that's the correct word) 'expenses' come from?
Thanks again for any help you can provide as I continue to learn!
createExpense: function (component, expense) {
        this.saveExpense(component, expense, function (response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var expenses = component.get("v.expenses");
                component.set("v.expenses", expenses);



Heidi ApaHeidi Apa
I will try to help, I think this trailhead module was something like this if I am not wrong:

The "component" refers to the component itself, the view of it where you have the form... or whatever. So thats why you do "component.get("v.expenses"): you get the atribute of list of expenses you had declared there (which is empty).
Then you push the values you returned to "expenses" and set the list of the component with it calling the component again "component.set("v.expenses", expenses);

I hope it helps :)