const digitsOnly = str => str.replace(/\D/g, '');
// или
const digitsOnly = str => (str.match(/\d/g) ?? []).join('');
// или
const digitsOnly = str => ''.concat(...str.matchAll(/\d/g));
// или
const digitsOnly = str => str.split(/\D/).join``;
// или
const digitsOnly = str =>
Array.prototype.reduce.call(
str,
(acc, n) => acc + ('0123456789'.includes(n) ? n : ''),
''
);
document.querySelectorAll('span').forEach(n => {
n.innerText = digitsOnly(n.innerText);
});
// или
for (const n of document.getElementsByTagName('span')) {
n.textContent = digitsOnly(n.textContent);
}
else
:if (state) {
this.x = 'c';
} else if (route) {
this.x = 'b';
} else if (prop) {
this.x = 'a';
}
this.x = [
[ state, 'c' ],
[ route, 'b' ],
[ prop, 'a' ],
].find(n => n[0])?.[1] ?? this.x;
this.x =
state ? 'c' :
route ? 'b' :
prop ? 'a' :
this.x;
switch (true) {
case !!state: this.x = 'c'; break;
case !!route: this.x = 'b'; break;
case !!prop: this.x = 'a'; break;
}
mounted () { ...
x
вычисляемым свойством. v-if="showModal"
на ref="modal"
, добавитьwatch: {
showModal(val) {
$(this.$refs.modal).modal(val ? 'show' : 'hide');
},
},
RegExp
в цепочке прототипов: x instanceof RegExp
.RegExp
конструктором значения: x?.constructor === RegExp
.const type = x => x == null ? x : x.constructor;
type() === undefined // true
type(null) === null // true
type(/./) === RegExp // true
type(666) === Number // true
type(type) === Function // true
const typename = x => x?.constructor.name ?? `${x}`;
typename() // 'undefined'
typename(null) // 'null'
typename(false) // 'Boolean'
typename(187) // 'Number'
typename('hello, world!!') // 'String'
typename(/./) // 'RegExp'
typename({}) // 'Object'
typename([]) // 'Array'
typename(document.body) // 'HTMLBodyElement'
typename(document.getElementsByClassName('xxx')) // 'HTMLCollection'
typename(new class XXX {}) // 'XXX'
typename(typename) // 'Function'
const newArr = arr.reduce((acc, n, i) => (acc.push(n + (acc[i - 1] ?? 0)), acc), []);
// или
const newArr = arr.reduce((acc, n) => (acc[0].push(acc[1] += n), acc), [ [], 0 ])[0];
// или
const newArr = arr.map((n, i, a) => eval(a.slice(0, i + 1).join('+')));
// или
const newArr = arr.map((n, i, a) => a.reduce((acc, m, j) => acc + m * (j <= i), 0));
arr.forEach((n, i, a) => a[i] += a[i - 1] ?? 0);
например на body
<button @click="onClick">
methods: {
onClick() {
document.body.classList.toggle('active');
},
},
<div :class="headerClass">
computed: {
headerClass() {
return в зависимости от this.$route;
},
},
methods: {
onKeyPress(e) {
if (/* нажато что надо */) {
this.$refs.searchInput.focus();
}
},
},
created() {
document.addEventListener('keypress', this.onKeyPress);
this.$on('hook:beforeDestroy', () => document.removeEventListener('keypress', this.onKeyPress));
},
('0' + date.getMonth()).slice(-2)
// или
`${date.getMonth()}`.padStart(2, 0)
date.toLocaleDateString('ru-RU').split('.').reverse().join('.')
// или
date.toLocaleDateString('ru-RU').replace(/(\d+)(\.\d+\.)(\d+)/, '$3$2$1')
this.props.options
return X; // тут НУЖНО ...
return new Promise(resolve => {
const img = new Image();
img.onload = () => resolve(true);
img.onerror = () => resolve(false);
img.src = 'тут ссылка на изображение';
});
document.querySelector('.menu').addEventListener('click', ({ target: t }) => {
if (t.tagName === 'A') {
const submenu = [...t.parentNode.children].find(n => n.classList.contains('sub-menu'));
if (submenu) {
submenu.classList.toggle('red');
}
}
// или
if (t.matches('a')) {
t.parentNode.querySelector(':scope > .sub-menu')?.classList.toggle('red');
}
});
for (const n of document.querySelectorAll('.menu a')) {
n.addEventListener('click', onClick);
}
function onClick() {
this.nextElementSibling?.classList.toggle('red');
}
// или
document.querySelectorAll('.sub-menu').forEach(function(n) {
n.previousElementSibling.addEventListener('click', this);
}, e => e.target.nextElementSibling.classList.toggle('red'));
data-target="#id-следующего-collapse-блока"
и data-toggle="collapse"
.