(re)studying javascript
- Have read the latest edition of "Learning javascript" at least three times.
- Have read "You Don't Know JS" at least once
- Have coded substantial amount of javascript already
- Now wanting to get some really fine techniques on javascript
Read Airbnb's javascript style guide:
- ⌛: 30 ~ 60 mins
- From 1. Types
- To 7.6 Never use arguments, opt to use rest syntax ...
1. Array.prototype.slice() in 4.5:
The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.
- Q:
sliceworks on objects? - A: Slice method can also be called to convert Array-like objects / collections to a new Array.. So you could do something like: Remember you need to specify the length, and the object keys should be indices. The following would result in nothing but an empty array:
const info = Array.prototype.slice.call({0: "Korea", 1: "KST", length: 2}) // ["Korea", "KST"]
But anyways, it's not recommended to useconst info = Array.prototype.slice.call({home: "Korea", time: "KST"}) // []
Array.prototype.slice, butArray.from.
2. Array.from in 4.6
- Q: So what can
Array.fromactually do with mapping? - A: This:
Array.fromcan receive a mapping function as the second argument, as such:const mapped = Array.from([1,2,3], elem=>elem*2) // 2,4,6
Read Airbnb's javascript style guide:
- ⌛: 30 ~ 60 mins
- From 7.7 Use default parameter syntax...
- To 15.5 Use braces to create blocks in case and default clauses...
- 14.2 Anonymous function expressions hoist their variable name, but not the function assignment.
- 14.3 Named function expressions hoist the variable name, not the function name or the function body.
Read Airbnb's javascript style guide:
- ⌛: 45 ~ 60 mins
- From 15.6 Ternaries should not be nested...
- To 30.2 No, but seriously: (The end)
- 18.4 You can use
FIXMEorTODOto annotate something in the comment. - 19.6 Use indentation when making long method chains...
- 21.1 Use semicolons.: You know, I don't really use semicolons in Javascript because the code tends to look cleaner. But airbnb certainly has suggested possible grounds for this, such as:
- "rules will become more complicated as new features become a part of JavaScript. Explicitly terminating your statements and configuring your linter to catch missing semicolons will help prevent you from encountering issues."
- But doesn't
prettierdo the job for filling out all the semicolons? I will have to dig into this a bit more.
- 22.2 Type casting for strings: don't use new keyword, but just 'String', because using the new keyword will let javascript recognize the variable as an object. (typeof)
- 23.4 Do not use trailing or leading underscores.
- 25.1 When attaching data payloads to events (whether DOM events or something more proprietary like Backbone events), pass an object literal (also known as a "hash") instead of a raw value.
- 29.1 Use
Number.isNaNinstead ofisNaN(same forNumber.isFinite)