Javascript Code Standards: Difference between revisions

From PM Wiki
Jump to navigation Jump to search
Line 44: Line 44:


<pre>
<pre>
function validate_form_fields(event, form_fields) {
function validate_form_fields(form_fields) {


     // code goes here
     // code goes here
}
}


button.addEventListener('click', validate_form_fields(event, form_fields));
button.addEventListener('click', validate_form_fields(form_fields));
</pre>
</pre>

Revision as of 17:10, 26 October 2022

Naming Conventions

Local Variables

Local variable names should all be lower case:

email

If the local variable name is made up of multiple words, use snake case:

email_address

Functions

Function names should all be lowercase:

update

If the function name is made up of multiple words, use snake case:

update_by_id

Classes

Class names should all be capitalized:

Vertical

If the class name is made up of multiple words, use Pascal case:

VerticalGroup

Function Definitions

Function definitions should all be full definitions with no shorthand/arrow notation:

function function_name(parameter_1, parameter_2) {

    // code goes here
}

In-line Callbacks

In-line callbacks should utilize shorthand/arrow notation:

button.addEventListener('click', (event) => {

    // code goes here
});

If the in-line callback is a reusable block of code that already has a function definition, use the function name instead of shorthand/arrow notation:

function validate_form_fields(form_fields) {

    // code goes here
}

button.addEventListener('click', validate_form_fields(form_fields));