In the salesforce trailhead "Lightning Web Components and Salesforce Data: Use Apex to Work with Data" we have the following code:
import { LightningElement, wire } from "lwc";
import NAME_FIELD from "@salesforce/schema/Account.Name";
import REVENUE_FIELD from "@salesforce/schema/Account.AnnualRevenue";
import INDUSTRY_FIELD from "@salesforce/schema/Account.Industry";
import getAccounts from "@salesforce/apex/AccountController.getAccounts";
const COLUMNS = [
{ label: "Account Name", fieldName: NAME_FIELD.fieldApiName, type: "text" },
{
label: "Annual Revenue",
fieldName: REVENUE_FIELD.fieldApiName,
type: "currency"
},
{ label: "Industry", fieldName: INDUSTRY_FIELD.fieldApiName, type: "text" }
];
export default class AccountList extends LightningElement {
columns = COLUMNS;
@wire(getAccounts)
accounts;
}
In this example, and in many others, variables such as COLUMNS
are established outside of the central class, rather than inside of it. This is then followed up by something line the line columns = COLUMNS
.
Why is this? Is there a technical reason for it? Is it just a syntax standard? Why isn't the code written more like this:
import { LightningElement, wire } from "lwc";
import NAME_FIELD from "@salesforce/schema/Account.Name";
import REVENUE_FIELD from "@salesforce/schema/Account.AnnualRevenue";
import INDUSTRY_FIELD from "@salesforce/schema/Account.Industry";
import getAccounts from "@salesforce/apex/AccountController.getAccounts";
export default class AccountList extends LightningElement {
columns = [
{ label: "Account Name", fieldName: NAME_FIELD.fieldApiName, type: "text" },
{ label: "Annual Revenue",fieldName: REVENUE_FIELD.fieldApiName,type: "currency" },
{ label: "Industry", fieldName: INDUSTRY_FIELD.fieldApiName, type: "text" }
];
@wire(getAccounts)
accounts;
}