====== Javascript ======
===== References =====
* [[https://www.typescriptlang.org/docs/handbook/utility-types.html|Utility types]]
Popular code styles
* [[https://github.com/airbnb/javascript|Airbnb]]
* [[https://github.com/standard/standard |Standard]]
* [[https://github.com/google/eslint-config-google|Google]]
===== Functional aspects =====
=== find ===
Function ''find()'' returns the first value of an array satisfying a given condition.
let array1 = [1,4,6,8,12];
let found = array1.find(el => {
return el > 10
}
);
//found = 12
=== map ===
Function ''map()'' applies a specified function to each element in the array.
var array1 = [1, 4, 9, 16];
const map1 = array1.map(x => Math.pow(x,2));
//map1=[1,16,81,256]
For more complicated functions
array1.map(x => {
//stuff with x
return x;
})
=== reduce ===
Function ''reduce()'' executes a given reducer function on every element of the array returning a single value. It consists of accumulator getting information from each element of the array consequently.
let array1 = [1, 2, 3, 4];
let r = array1.reduce((acc, elem) => acc + elem)
//r = 10
Initial value ''initVal'' could be specified by
array1.reduce((acc,elem) => acc + elem, initVal);
=== every ===
Function ''every()'' checks if a given condition applies to every element of the array.
const array1 = [true, false, true];
if(array1.every(Boolean)) {
//all conditions met
}
=== filter ===
Returns a subset of the array satisfying certain conditions
const data = ["hotel","california", "baby"];
let newdata = data.filter(e => e.length > 5);
===== Object orientated aspects =====
=== Constructor ===
Constructor shortcut without manually assigning properties to the new instance.
export class CartItem {
id: number;
name: string;
description: string;
unitPrice: number;
coverPhoto: string;
quantity: number;
constructor(data: any) {
Object.assign(data)
}
}
New instance of the class with specified properties can now be created by
CartItem({'id': 1, 'name': 'Paper towel', ...})
===== Syntax =====
Template literals
const x = 10
console.log("x = ${x}")
const y = `x = ${x}`
Multiline string
const s = "SELECT * \
FROM table \
WHERE ...";
const t = `one \
two`
===== Frameworks =====
* [[angular|Angular]]
* [[react|React]]