Published | 25/10/2016 |
---|---|
Last Updated | 20/11/2024 |
This will discuss some of JS style guide proposal made for my project:
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
;
In ES6 we don't have to use ;
at the end of a statementconst
for variable that doesn't change, let
for variable that will be mutated. Avoid var
.// bad
var bar = "baz"
var foobar = "foo" + bar
// good
const foobar = `foo${bar}`
Generai rules:
$
AJAX
.getJson()
or .get()
, always use $.ajax()
success
, error
, complete
events// 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 (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.