Contents
Operators[edit]
'==' or '==='[edit]
Крайне рекомендуется использовать только операторы строгого равенства
used in #indexOf
iif, .?[edit]
null-coalescing as logical ||
&&, ||[edit]
&& evaluates as long as the statement is true. If it is true, it returns the last value. If it is false, it returns the first value that evaluated to false. That may be 0, null, false etc. || returns the first value that evaluates to true.
Types casting[edit]
!!myString; // convert to bool
+myString; // convert to number
~~myNumber; // double NOT bitwise operator is faster substitute for Math.floor()
TypeScript vs CSharp[edit]
any is like dynamic and unknown is like object
typeof[edit]
- Array.isArray
Strings[edit]
use Regex where possible
- substring
String method substring or slice
- last characters
/substring$/.test(str)
- last n characters
slice.(0, -n) - allows you to make the second argument negative
Hashed lists and dictionaries[edit]
the JavaScript Object is implemented with a hash table iteration may be done by for(let key in dict)
Remove property from object[edit]
OOP[edit]
Every object (including functions) has this internal property called prototype. It can only be set at object creation time, either with new, with Object.create, or based on the literal (functions default to Function.prototype, numbers to Number.prototype, etc.). It can only be read with Object.getPrototypeOf(someObject). There is no other way to set or read this value... Functions, in addition to the hidden prototype property, also have a property called prototype, and it is this that you can access, and modify
Loops[edit]
in TypeScript iteration over elements is for(let i of list) construction
Filter[edit]
- Array.prototype.filter
good support
- Array.prototype.find - returns the value of the first element in the array that satisfies the provided testing function
bad support: no IE, Safari 7.1
Methods[edit]
toFixed[edit]
toFixed() з javascript використовуємо лише для внутрішніх розрахунків, для презентаційних цілей (показати юзеру) її ніззя, бо вона не враховує регіональні налаштування формату чисел
DOM and browser[edit]
querySelector[edit]
good support including Safari 3.1+, or IE8+
getBoundingClientRect[edit]
good support
Page redirect[edit]
як правильно зробити редірект через window.location
- GetAppRoot() - кастомна ЖС функція
- window.location.search
asynchronous[edit]
setTimeout и setInterval... Рекурсивный setTimeout позволяет задать задержку между выполнениями более точно, чем setInterval.[edit]
Event Loop[edit]
async/await[edit]
bad support, no IE, no Windows 8.1, but polyfilled in core-js
Pages in category "JS coding guide"
The following 6 pages are in this category, out of 6 total.