let todoList = {
todoItems: [1, 2, 3, 4],
[Symbol.iterator] () {
console.log('начинаем перебирать массив по кругу!')
let i = -1;
return {
next: () => {
i += 1;
if (i > 10) {
return {
done: true,
value: null,
}
}
return {
done: false,
value: this.todoItems[i % this.todoItems.length],
}
}
};
}
};
for (let item of todoList) {
console.log(item);
}
let iterableTodoList = todoList[Symbol.iterator];
console.log(
'А это символ-итератор в переменной, применим его к другому объекту', iterableTodoList
);
let todoList2 = {
todoItems: ['a', 'b', 'c'],
[Symbol.iterator]: iterableTodoList,
};
for (let item of todoList2) {
console.log(item);
}
<mat-card [formGroupName]="body">
Cannot find control with unspecified name attribute
в консоли не смутили чтоли?const focusBtn = document.getElementById('focusBtn');
let listenerFocus;
focusBtn.addEventListener('focus', () => {
listenerFocus = listenEnter();
});
focusBtn.addEventListener('blur', () => {
window.removeEventListener('keydown', listenerFocus);
});
function listenEnter() {
const listener = (evt) => {
if (evt.keyCode === 13) {
openModalMap();
}
}
window.addEventListener('keydown', listener);
return listener;
}
function openModalMap() {
document.getElementById('modal').classList.toggle('hidden');
}
ctx.replyWithVoice({
source: request(audioLink)
})
function cardAnimate(){
[
document.getElementById('card__title'),
document.getElementById('card__subtitle'),
document.getElementById('btn'),
].forEach(el => el && TweenLite.to(el, .75, {transform:"translateX(0)"}));
}
import { TestComponent } from 'components/test.vue';
let name = 'vasya';
if (cond1) {
name = 'pavlek';
}
if (cond2) {
name = 'semen';
}
const vasya = (() => {
switch (true) {
case cond2: return 'semen';
case cond1: return 'pavlek';
default: return 'vasya';
}
})()
const keys = ["name", "my-city-id", "family-status",
"Number", "e-mail"
];
for (let id of keys) {
// выставил
let input = document.getElementById(id);
input.value = localStorage.getItem(id);
console.log("сохранено");
// закинул
let fun = function(id, input) {
input.addEventListener("change", function() {
localStorage.setItem(id, input.value);
});
};
fun(id, input);
}
[
'e-mail',
'family-status',
'my-city-id',
'name',
'Number',
].forEach((id) => {
/** @type {HTMLInputElement} */
const inputElement = document.getElementById(id);
inputElement.value = localStorage.getItem(id);
inputElement.addEventListener(
'change',
() => localStorage.setItem(id, inputElement.value),
);
});
const p = {
a: 11
};
const obj = {
b: 22
};
Object.setPrototypeOf(obj, p);
obj.a //-> 11
obj.b //-> 22
console.log(Object.keys(obj).length); //-> 1
console.log((() => {
let length = 0;
for (const k in obj) {
length++;
}
return length;
})()); //-> 2 , т.е. a и b
console.log((() => {
let length = 0;
for (const k in obj) {
if (obj.hasOwnProperty(k)) {
length++
}
}
return length;
})()); //-> 1
'a' in obj //-> true
obj.hasOwnProperty('a') //-> false
for...in
в паре с hasOwnProperty
можно считать устаревшей конструкцией, вместо нее используются методы Object
public changeObject() {
this.isDivShown = true;
setTimeout(() => this.changeClasses());
}
private changeClasses() {
this.el1.nativeElement.className = 'myCSSclass_1';
this.el2.nativeElement.className = 'myCSSclass_2';
}
<div [class.myCSSclass_1]="isDivShown">Element_1</div>
<div *ngIf="isDivShown" [class.myCSSclass_2]="isDivShown">Element_2</div>
<button (click)="isDivShown = true">Change the objects</button>
function objectMaker(schema, src) {
return Object.keys(schema).reduce((acc, el) => {
if (src.hasOwnProperty(el)) {
acc[el] = src[el]
}
return acc;
}, {})
}
const mySchema = {
author: 'string',
description: 'string',
}
const source = {
author: 'Vasya',
garbage: 'dfsfsdf',
}
const authorOne = objectMaker(mySchema, source);