Javascript Typescript - ayaohsu/Personal-Resources GitHub Wiki
Enumeration
enum
, with no exception, is an object in javascriptenum Side { BUY, SELL}
is an object of:
{
BUY: 0,
0: "BUY",
SELL: 1,
1: "SELL"
}
Template literals
- string literals allowing embedded expressions.
`string text ${expression} string text`
- enclosed by the back-tick instead of double or single quotes.
- [FYI] expression can be a literal value (a string or a number), a variable, a built-in value
Javascript Loop
-
for loop for (statement 1; statement 2; statement 3) { code block to be executed }
-
forEach Array prototype array.forEach(function(currentValue, index, arr))
-
for of for (variable of iterable) { statement }
Three dots
- rest parameters: if the last parameter is prefixed with ..., it becomes an array whose elements are supplied by the actual arguments passed to the function
function multiply(multiplier, ...theArgs) {
return theArgs.map(function(element) {
return multiplier * element;
})
}
let multiplication = multiply(5, 3, 2, 7);
-
theArgs is treated as an array
-
spread syntax: to expand an array (or any iterable) to places where zero or more arguments are expected
let dateFields = [1989, 6, 7];
let date = new Date(...dateFields);
Regular Experssions
- Patterns used to match charater combinations in strings
let re0 = /abc/;
let re1 = new RegExp('abc');
- Modifiers: give the property of the search; ex:
i
: case-insensitive,g
: global search (find all matches rather than stopping at the first match)
let re0 = /abc/g;
let re1 = new RegExp('abc', i);
-
\
: Non-special <-> Special -
^
: Matches beginning of input;$
: End of input -
*
: Matches the preceding expression 0 or more times. Equivalent to {0,} -
+
: Matches the preceding expression 1 or more times. Equivalent to {1,} -
\w
: Any alphanumeric character, including the underscore. Equivalent to[A-Za-z0-9\]
-
\s
: Matches both space character and tab -
\t
: Matches tab -
When finding out whether a pattern is found in a string, use
test
orsearch
let found = re0.test('abbbc'); // this should return false
Object Literal for ES6
- Object keys can be dynamically assigned in ES6 by placing an expression in [ square brackets ].
const key = "one";
let object = {
[key]: 1,
"two": 2
}
// obj.one = 1, obj.two = 2
Understanding "this"
https://javascriptissexy.com/understand-javascripts-this-with-clarity-and-master-it/