Published 25/10/2016
Last Updated 20/11/2024

This will discuss some of JS style guide proposal made for my project:

Use ES6 features effectively

We write ES6 so we should get the best out of it. For a quick recap of ES6 features, check this http://es6-features.org/

Here are some quick rules to follow

// bad
var bar = "baz"
var foobar = "foo" + bar

// good
const foobar = `foo${bar}`

jQuery

Generai rules:

AJAX

// promise examples
$.ajax({ ... }).then(successHandler, failureHandler);

// or
var jqxhr = $.ajax({ ... });
jqxhr.done(successHandler);
jqxhr.fail(failureHandler);

// or
$.ajax({
  url: url,
  type: "GET"
  data: {},
  dataType: "JSON"
})
.done((data, textStatus, jqXHR) => {
  // success callbacks
})
.fail((jqXHR, textStatus, errorThrown) => {
  // error callbacks
})
.always((data|jqXHR, textStatus, jqXHR|errorThrown) => {
  // complete callbacks
})

Lodash

Lodash (https://lodash.com/docs/) provides handful of methods that helps with collection/string manipulation and other ultilities.

A few frequetly used functions such as _.uniqueId to generate unique id (e.g. for new records), .remove to remove certain elements that match a predicate from an array,  .difference to compare 2 collections.