(→Async) |
|||
Line 112: | Line 112: | ||
* window.location.search | * window.location.search | ||
− | = | + | = [[asynchronous]] = |
* Promise.all() | * Promise.all() | ||
bad support, no IE, no Windows 8.1, but polyfilled in [[core-js]] | bad support, no IE, no Windows 8.1, but polyfilled in [[core-js]] | ||
== [https://learn.javascript.ru/settimeout-setinterval setTimeout и setInterval] == | == [https://learn.javascript.ru/settimeout-setinterval setTimeout и setInterval] == |
Latest revision as of 07:57, 10 August 2019
Contents
Operators[edit | edit source]
'==' or '==='[edit | edit source]
Крайне рекомендуется использовать только операторы строгого равенства
used in #indexOf
iif, .?[edit | edit source]
null-coalescing as logical ||
&&, ||[edit | edit source]
&& 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 | edit source]
- +myString; // convert to number
- !!myString; // convert to bool
typeof[edit | edit source]
- Array.isArray
Classes[edit | edit source]
В ES6 появляются «настоящие» классы
Strings[edit | edit source]
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 | edit source]
iteration may be done by for(let key in dict)
Remove property from object[edit | edit source]
Arrays[edit | edit source]
contains[edit | edit source]
- maybe use Array#includes with polyfill from core-js as ECMAScript 7+ proposal
- if (list.filter(s => s.Id === element.Id).length > 0)
- core-js dict.includes
- this.value = !('value' in data) || data.value
indexOf[edit | edit source]
- the best!! Array#indexOf
- Array#includes, which does exactly that, is widely supported, and has a polyfill for older browsers. You can also use Array#indexOf, which is less direct, but doesn't require Polyfills for out of date browsers. jQuery offers $.inArray, which is functionally equivalent to Array#indexOf... Lodash: _.includes
join into string[edit | edit source]
analogs: JSON.stringify() or .toString() method from prototype
function(obj){return JSON.stringify(obj)}).join(' '))
map[edit | edit source]
Array.map() not supported in IE < 9 on jQuery: ($.map(array,
var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
return num * 2;
});
// doubles is now [2, 8, 18]
add[edit | edit source]
push or spread of ES6[edit | edit source]
several elements that array did not contain[edit | edit source]
list.push(newList.filter(
newEl => list.filter(oldEl => oldEl.Id === newEl.Id).length === 0));
//or
list = list.concat(newList.filter(
newEl => list.filter(oldEl => oldEl.Id === newEl.Id).length === 0));
remove[edit | edit source]
How to remove element from array[edit | edit source]
- array.splice(index, 1);
How to remove array from other array[edit | edit source]
for (let i = arr.length - 1; i >= 0; i--) {
const j = toRemove.indexOf(arr[i]);
if (j >= 0) {
arr.splice(i, 1);
}
};
Loops[edit | edit source]
in TypeScript iteration over elements is for(let i of list) construction
Filtering, LINQ[edit | edit source]
- 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 | edit source]
toFixed[edit | edit source]
toFixed() з javascript використовуємо лише для внутрішніх розрахунків, для презентаційних цілей (показати юзеру) її ніззя, бо вона не враховує регіональні налаштування формату чисел
DOM and browser[edit | edit source]
querySelector[edit | edit source]
good support including Safari 3.1+, or IE8+
getBoundingClientRect[edit | edit source]
good support
Page redirect[edit | edit source]
як правильно зробити редірект через window.location
- GetAppRoot() - кастомна ЖС функція
- window.location.search
asynchronous[edit | edit source]
- Promise.all()
bad support, no IE, no Windows 8.1, but polyfilled in core-js