const a = { x: 1 };
a.__proto__.x = 2;
const b {};
console.log(a.x, b.x);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
class CustomError extends Error {
constructor(message) {
super(message);
this.name = 'CustomError';
}
}
try {
try {
throw new CustomError('inner error');
} catch (e) {
console.log(e.name);
throw new Error('outer error');
}
} catch (e) {
console.log(e.message);
console.log(e instanceof CustomError);
}
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const weakSet = new WeakSet();
let obj1 = { id: 1 };
let obj2 = { id: 2 };
let obj3 = obj1;
weakSet.add(obj1);
weakSet.add(obj2);
const results = [
weakSet.has(obj1),
weakSet.has(obj3),
weakSet.has({ id: 2 }),
weakSet.has(obj2)
];
obj1 = null;
console.log(results);
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
class DataProcessor {
constructor(transform) {
this.transform = transform;
}
process(data) {
return this.transform(data);
}
}
const multiply = x => x * 2;
const addTen = x => x + 10;
const toString = x => `Result: ${x}`;
const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x);
const processor = new DataProcessor(compose(toString, addTen, multiply));
console.log(processor.process(5));
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const obj = {
name: 'Sarah',
getName() { return this.name; },
getNameArrow: () => this.name
};
const { getName, getNameArrow } = obj;
const boundGetName = obj.getName.bind(obj);
console.log(getName());
console.log(getNameArrow());
console.log(boundGetName());
console.log(obj.getName());
console.log(obj.getNameArrow());
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const target = { name: 'Sarah', age: 25 };
const handler = {
get(obj, prop) {
if (prop in obj) {
return obj[prop].toString().toUpperCase();
}
return `Property ${prop} not found`;
},
set(obj, prop, value) {
obj[prop] = value * 2;
return true;
}
};
const proxy = new Proxy(target, handler);
proxy.salary = 50000;
console.log(proxy.name);
console.log(proxy.age);
console.log(proxy.salary);
console.log(proxy.city);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const obj = { a: 1, b: { c: 2 } };
const frozen = Object.freeze(obj);
frozen.a = 99;
frozen.b.c = 88;
frozen.d = 77;
const sealed = Object.seal({ x: 10, y: 20 });
sealed.x = 30;
sealed.z = 40;
delete sealed.y;
console.log(obj.a, obj.b.c, obj.d);
console.log(sealed.x, sealed.y, sealed.z);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const userInput = "<script>alert('xss')</script>";
const sanitized = userInput.replace(/<script[^>]*>.*?<\/script>/gi, '');
const users = new Map();
users.set('admin', { password: 'secret123', role: 'admin' });
users.set('guest', { password: 'guest', role: 'user' });
function authenticate(username, password) {
const user = users.get(username);
return user && user.password === password ? user.role : null;
}
const role1 = authenticate('admin', 'secret123');
const role2 = authenticate('guest', 'wrong');
const role3 = authenticate('hacker', 'secret123');
console.log(sanitized);
console.log(role1, role2, role3);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const obj = { a: 1, b: 2, c: 3 };
const result = Object.values(obj).reduce((acc, curr) => acc * curr, 1);
console.log(result);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const str = 'JavaScript';
const result1 = str.slice(-6, -2);
const result2 = str.substring(-6, -2);
const result3 = str.substr(-6, 4);
const combined = [result1, result2, result3];
const final = combined.map(s => s || 'empty').join(' | ');
console.log(final);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const original = {
name: 'Sarah',
scores: [85, 92, 78],
details: {
age: 25,
city: 'Portland'
}
};
const copy1 = { ...original };
const copy2 = JSON.parse(JSON.stringify(original));
copy1.name = 'Emma';
copy1.scores.push(95);
copy1.details.age = 30;
console.log(original.name, original.scores.length, original.details.age);
Ответ:
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 userInput = "<script>alert('xss')</script>";
const sanitized = userInput.replace(/<script[^>]*>.*?<\/script>/gi, '');
const users = new Map();
users.set('admin', { password: 'secret123', role: 'admin' });
users.set('guest', { password: 'guest', role: 'user' });
function authenticate(username, password) {
const user = users.get(username);
return user && user.password === password ? user.role : null;
}
const role1 = authenticate('admin', 'secret123');
const role2 = authenticate('guest', 'wrong');
const role3 = authenticate('hacker', 'secret123');
console.log(sanitized);
console.log(role1, role2, role3);
Ответ:
JavaScript test | #JavaScript
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
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 numbers = [1, 2, 3, 4, 5];
const result = numbers
.filter(n => n % 2 === 0)
.map(n => n * 2)
.reduce((acc, n) => acc + n, 0);
const original = numbers.slice();
original.reverse();
const flattened = [[1, 2], [3], [4, 5]].flat();
const found = flattened.find(n => n > 3);
console.log(result);
console.log(original.length);
console.log(found);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function main({ x, y } = { x: 1, y: 2 }) {
console.log(x, y);
}
main({ x: 5 });
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
try {
const obj = null;
obj.property = 'value';
} catch (e) {
console.log(e.name);
}
try {
undeclaredVariable;
} catch (e) {
console.log(e.name);
}
try {
JSON.parse('invalid json');
} catch (e) {
console.log(e.name);
}
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const target = { name: 'Sarah', age: 25 };
const handler = {
get(obj, prop) {
if (prop in obj) {
return obj[prop];
}
return `Property '${prop}' not found`;
},
set(obj, prop, value) {
obj[prop] = value.toString().toUpperCase();
return true;
}
};
const proxy = new Proxy(target, handler);
proxy.city = 'boston';
console.log(proxy.name);
console.log(proxy.city);
console.log(proxy.country);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function Person(name) {
this.name = name;
this.sayName = () => console.log(this.name);
}
const person1 = new Person('David');
const person2 = { name: 'Not David', sayName: person1.sayName };
person2.sayName();
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM