console.log('Widget with id and'.indexOf('id'));
Widget with id and
const sentence = 'Widget with id and';
const words = sentence.match(/\w+/g); // ['Widget', 'with', 'id', 'and']
console.log(words.indexOf('id'));
const values = [...document.querySelectorAll('[data-name]')].map(entry => entry.dataset.name);
const date = new Date();
const time = Math.floor(date.getTime() / 1000);
const $1day = 24 * 60 * 60;
const filtered = array.filter(entry => {
if (entry.hasOwnProperty('last_seen')) {
return entry.last_seen.time > (time - $1day);
}
return false;
});
<button type="button" class="close" aria-label="Close">...</button>
const alerts = document.querySelectorAll('.alert');
for (const alert of alerts) {
const closeButton = alert.querySelector('.close');
if (closeButton !== null) {
closeButton.addEventListener('click', event => {
event.preventDefault();
alert.style.setProperty('display', 'none');
});
}
}
if (!('addEventListener' in NodeList)) {
NodeList.prototype.addEventListener = function (...args) {
for (const node of this.values()) {
node.addEventListener(...args);
}
}
}
if (!('removeEventListener' in NodeList)) {
NodeList.prototype.removeEventListener = function (...args) {
for (const node of this.values()) {
node.removeEventListener(...args);
}
}
}
const elements = document.querySelectorAll('some_selector');
elements.addEventListener('event_type', ...);
class Canv {
constructor(width, height) {
this.width = width;
this.height = height;
}
...
}
const canv = new Canv(50, 50);
class Canv {
constructor({ width, height }) {
this.width = width;
this.height = height;
}
...
}
const canv = new Canv({
width: 50,
height: 50
});
const resize = () => {
[canvas.width, canvas.height] = [innerWidth, innerHeight];
};
const init = () => {
resize();
};
window.addEventListener('DOMContentLoaded', init);
window.addEventListener('resize', resize);
preapreDestroy
:class Some {
constructor() {
this.interval = setInterval(this.method, 100);
}
method() {
console.log(`Date.now: ${Date.now()}`);
}
prepareDestroy() {
clearInterval(this.interval);
}
}
const sleep = duration => new Promise(resolve => setTimeout(resolve, duration));
(async () => {
let some = new Some();
await sleep(3000);
some.prepareDestroy();
some = null;
})();
setInterval
на requestAnimationFrame
gulp.src('src/**/*.html')
.pipe(gulp.dest('build/'));