https://learn.javascript.ru/prototype
Другими словами, прототип – это «резервное хранилище свойств и методов» объекта, автоматически используемое при поиске.
var data = Object.create(null); data.text = "Привет"; alert(data.text); // Привет alert(data.toString); // undefined
Объект, создаваемый при помощи Object.create(null) не имеет прототипа, а значит в нём нет лишних свойств. Для коллекции – как раз то, что надо.
- Если ли разница в порядке then catch finally у промисов
const arr = [ 5, 3, 4, 2, 3, 7, 5, 6 ];
const findDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) !== index)
const duplicates = findDuplicates(arr);
console.log(duplicates);
function iterationCopy(src) {
let target = {};
for (let prop in src) {
if (src.hasOwnProperty(prop)) {
target[prop] = src[prop];
}
}
return target;
}
const source = {a:1, b:2, c:3};
const target = iterationCopy(source);
console.log(target); // {a:1, b:2, c:3}
// Check if clones it and not changing it
source.a = 'a';
console.log(source.a); // 'a'
console.log(target.a); // 1
function jsonCopy(src) {
return JSON.parse(JSON.stringify(src));
}
const source = {a:1, b:2, c:3};
const target = jsonCopy(source);
console.log(target); // {a:1, b:2, c:3}
// Check if clones it and not changing it
source.a = 'a';
console.log(source.a); // 'a'
console.log(target.a); // 1
function bestCopyEver(src) {
return Object.assign({}, src);
}
const source = {a:1, b:2, c:3};
const target = bestCopyEver(source);
console.log(target); // {a:1, b:2, c:3}
// Check if clones it and not changing it
source.a = 'a';
console.log(source.a); // 'a'
console.log(target.a); // 1
Английские гласные и согласные
function isVowel(c) {
return ['a', 'e', 'i', 'o', 'u', 'y'].indexOf(c.toLowerCase()) !== -1;
}
let consonants = [];
for (let i = 'a'.charCodeAt(0); i <= "z".charCodeAt(0); i++) {
if (!isVowel(String.fromCharCode(i))) consonants.push(String.fromCharCode(i));
}
console.log(consonants);
Случайные имена
let vowels = [];
let consonants = [];
for (let i = 'a'.charCodeAt(0); i <= "z".charCodeAt(0); i++) {
let c = String.fromCharCode(i);
if (['a', 'e', 'i', 'o', 'u', 'y', 'j'].indexOf(c.toLowerCase()) !== -1) {
vowels.push(String.fromCharCode(i));
} else {
consonants.push(String.fromCharCode(i));
}
}
let word = '';
for (let i = 1; i <= 4; i++) {
word += consonants[Math.floor(Math.random() * consonants.length)];
word += vowels[Math.floor(Math.random() * vowels.length)];
}
word += consonants[Math.floor(Math.random() * consonants.length)];
word = word[0].toUpperCase() + word.slice((1));
console.log(word);
var evt2 = new PointerEvent('pointerup');
document.getElementById("deckgl-overlay").dispatchEvent(evt2);