Является ли нормальной практикой создание нового метода , включающего в себя эти три метода
buildButton(() => {
this.drawSomeButtons();
this.changeSomeVariables();
this.doSomethingElse();
});
x
- свойство экземпляра, другой - статический геттер:class Base {
constructor() {
this.x = 3;
}
static get x() {
return 1.5;
}
}
function Base() {
this.x = 3;
}
Base.x = 1.5;
// или
function Base() {}
Base.prototype.x = 3;
Base.x = 1.5;
class Base {
x = 3;
static x = 1.5;
}
this.steppers[index]=this.steppers[index]+this.step_go
this.$set(this.steppers, index, this.steppers[index] + this.step_go)
$str = "философски нагруженная";
preg_match("/.{50}$str.{50}/u", $text, $match);
const getValue = (obj, index = 0) =>
Object.values(obj)[index];
function getValue(obj, index = 0) {
for (const k in obj) {
if (obj.hasOwnProperty(k) && !index--) {
return obj[k];
}
}
}
$('.list_add_room').on('click', '.butt_edit', function() {
const $el = $(this).closest('.item_contain_list').find('p');
$('.list_add_room [contenteditable="true"]').not($el).removeAttr('contenteditable');
$el.attr('contenteditable', (i, val) => val !== 'true').focus();
});
$('.list_add_room').on('click', '.butt_edit', function() {
$(this).closest('.item_contain_list').find('p').attr('contenteditable', 'true').focus();
}).on('blur', '[contenteditable="true"]', function() {
$(this).removeAttr('contenteditable');
});
$(function() { })
, или воспользуйтесь делегированием:$(document).on('change', 'input[type="radio"]', function() {
$('.color').text($(this).val());
});
я открыл доселе неизвестный it-сообществу баг...
... или где-то далеко по мне плачет один учебник по js?
setInterval(this.tick, interval);
наsetInterval(this.tick.bind(this), interval);
setInterval(() => this.tick(), interval);
document.querySelectorAll('.u-combolist label').forEach(n => {
if (n.textContent.toLowerCase().indexOf(genre) != -1) {
n.previousElementSibling.checked = true;
}
});
$('.u-combolist label')
.filter((i, n) => $(n).text().toLowerCase().includes(genre))
.prev()
.prop('checked', true);
const $el = $('.field');
const el = document.querySelector('.field');
$el.append(document.createTextNode(str));
// или
$el.append(new Text(str));
// или
$el.append(str.replace(/</g, '<'));
// или
el.append(str);
$el.append($(str).text());
// или
const div = document.createElement('div');
div.innerHTML = str;
el.insertAdjacentText('beforeend', div.innerText);
// или
el.append(document.createRange().createContextualFragment(str).textContent);
// или
el.append(new DOMParser().parseFromString(str, 'text/html').body.textContent);
.black { background: black; }
.white { background: white; }
const rows = 8;
const cols = 8;
document.querySelector('.info').innerHTML = `
<table>${Array.from({ length: rows }, (_, i) => `
<tr>${Array.from({ length: cols }, (_, j) => `
<td class="${(i ^ j) & 1 ? 'black' : 'white'}"></td>`).join('')}
</tr>`).join('')}
</table>`;
const table = document.createElement('table');
for (let i = 0; i < rows; i++) {
const tr = table.insertRow();
for (let j = 0; j < cols; j++) {
tr.insertCell().classList.add([ 'white', 'black' ][(i ^ j) & 1]);
}
}
document.querySelector('.info').append(table);
to
конкретный элемент. Например:const
ease = Power0.easeNone,
yoyo = true;
document.querySelectorAll('svg').forEach((n, i) => {
const
TopWave = new TimelineLite(),
el = n.querySelector('.wave'),
delay = i * (1 + Math.random() * 2);
TopWave
.to(el, 8, { delay, morphSVG: '.wave-one', yoyo, ease })
.to(el, 7, { delay, morphSVG: '.wave-two', yoyo, ease })
.to(el, 8, { delay, morphSVG: '.wave-three', yoyo, ease,
onComplete: () => TopWave.restart()
});
});
.test1
, .test2
, .test3
- убрать цифры, пусть везде будет просто .test
const wrapper = document.querySelector('.wrapper');
wrapper.addEventListener('click', ({ target: t }) => {
const { test } = t.dataset;
if (test) {
const toRemove = Array
.from(t.closest('.test').children, n => n.dataset.test)
.filter(n => n !== test);
wrapper.classList.remove(...toRemove);
wrapper.classList.toggle(test);
document.querySelector('#classlist').innerHTML = wrapper.classList;
}
});