Является ли нормальной практикой создание нового метода , включающего в себя эти три метода
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);
function setChecked(value) {
value = value.toLowerCase();
for (const n of document.querySelector('.u-combolist').children) {
n.querySelector('input').checked = n
.querySelector('label')
.innerText
.toLowerCase()
.includes(value);
}
}
function setChecked(value) {
let test = () => false;
try {
test = RegExp.prototype.test.bind(RegExp(value, 'i'));
} catch (e) {}
document.querySelectorAll('.u-combolist label').forEach(n => {
n.previousElementSibling.checked = test(n.textContent);
});
}
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 output = document.querySelector('#classlist');
const container = document.querySelector('.wrapper');
const rowSelector = '.test';
const key = 'test';
const itemSelector = `[data-${key}]`;
container.addEventListener('click', function({ target: t }) {
if (t = t.closest(itemSelector)) {
const toRemove = Array.prototype.reduce.call(
t.closest(rowSelector).querySelectorAll(itemSelector),
(acc, n) => (n !== t && acc.push(n.dataset[key]), acc),
[]
);
this.classList.remove(...toRemove);
this.classList.toggle(t.dataset[key]);
output.textContent = this.classList;
}
});
.tickFormat(d3.timeFormat('%d %B'))