let obj = { a: 1 };
Object.freeze(obj);
console.log(Object.isFrozen(obj));
console.log(obj.a = 2);
console.log(obj.a);Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const user = {
name: 'Sarah',
age: 25,
getName() { return this.name; },
getAge: () => this.age
};
const methods = {
regular: user.getName,
arrow: user.getAge
};
console.log(methods.regular());
console.log(methods.arrow());
console.log(user.getName());
console.log(user.getAge());Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
let obj1 = { name: 'Sarah' };
let obj2 = { name: 'Mike' };
let obj3 = { ref: obj1 };
obj1.circular = obj1;
obj2.partner = obj3;
obj3.partner = obj2;
let weakMap = new WeakMap();
weakMap.set(obj1, 'data1');
weakMap.set(obj2, 'data2');
obj1 = null;
obj2 = null;
console.log(weakMap.has(obj3.ref));
console.log(obj3.partner.name);Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
var str="My name is John";
var words1=str.split(" ",3);
console.log("words1:",words1);
var words2=str.split(" ",5);
console.log("words2:",words2);
Ответ:
words1:[ 'My', 'name', 'is' ]
words2:[ 'My', 'name', 'is', 'John' ]
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function createCounter() {
let count = 0;
return {
increment: () => ++count,
decrement: () => --count,
get: () => count
};
}
const counter1 = createCounter();
const counter2 = createCounter();
counter1.increment();
counter1.increment();
counter2.increment();
console.log(counter1.get(), counter2.get());
const { increment, get } = counter1;
increment();
console.log(get());Ответ:
2 1 3
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
async function foo() {
console.log('Start');
await Promise.resolve().then(() => {
console.log('Inside Promise');
});
console.log('End');
}
foo();
console.log('Outside');Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
const promise1 = Promise.resolve('first');
const promise2 = new Promise(resolve => {
resolve('second');
});
const promise3 = Promise.resolve().then(() => 'third');
async function test() {
console.log('start');
const result1 = await promise1;
console.log(result1);
const result2 = await promise2;
console.log(result2);
const result3 = await promise3;
console.log(result3);
console.log('end');
}
test();Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const person = {};
person.name.toUpperCase();
console.log(person.name);Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const matrix = [
[2, 4],
[6, 8],
];
const result = matrix.reduceRight((acc, row) => acc.concat(row.map(num => num * 2)), []);
console.log(result);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const firstArrayData = [ 'JavaScript', 'Universe' ];
const secondArrayData = [ 'JavaScript', 'Universe' ];
console.log(firstArrayData == secondArrayData);
console.log(firstArrayData === secondArrayData);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const sym1 = Symbol("id");
const sym2 = Symbol("id");
console.log(sym1 === sym2);Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const original = { a: 1, b: { c: 2 } };
const shallow = { ...original };
const deep = JSON.parse(JSON.stringify(original));
shallow.a = 10;
shallow.b.c = 20;
deep.a = 100;
deep.b.c = 200;
const frozen = Object.freeze({ x: 1, y: { z: 2 } });
frozen.x = 99;
frozen.y.z = 99;
console.log(original.a, original.b.c, frozen.x, frozen.y.z);Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
let funcs = [];
for (var i = 0; i < 3; i++) {
funcs.push(() => i);
}
console.log(funcs[0]());
console.log(funcs[1]());
console.log(funcs[2]());
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
async function fetchData() {
console.log('1');
return Promise.resolve('data');
}
async function processData() {
console.log('2');
const result = await fetchData();
console.log('3');
return result;
}
console.log('4');
processData().then(() => console.log('5'));
console.log('6');Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const person = {
name: "John",
greet: function() {
const getMessage = () => `Hello, ${this.name}`;
return getMessage();
}
};
console.log(person.greet());Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const arr = new Array(1000000).fill(0).map((_, i) => i);
const results = [];
function method1() {
return arr.filter(x => x % 2 === 0).map(x => x * 2).slice(0, 3);
}
function method2() {
const result = [];
for (let i = 0; i < arr.length && result.length < 3; i++) {
if (arr[i] % 2 === 0) {
result.push(arr[i] * 2);
}
}
return result;
}
console.log(method1().join(','));
console.log(method2().join(','));
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
function* infiniteGenerator() {
let i = 0;
while (true) {
yield i++;
}
}
const weakMap = new WeakMap();
const gen = infiniteGenerator();
weakMap.set(gen, gen.next().value);
const result = weakMap.get(gen) + gen.next().value;
console.log(result);Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const words = ['a', 'b', 'c'];
const result = words.concat(1, 2, 3);
console.log(result);
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
function foo() {
return typeof arguments;
};
console.log(foo(1, 2, 3));
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const a = { value: 1 };
const b = Object.create(a);
b.value = 2;
console.log(b.value);
console.log(a.value);Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM