const str1 = 'Hi\nthere';
const str2 = 'Hi
there';
str1 === str2;
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
var a = 5;
function test() {
console.log(a);
var a = 10;
console.log(a);
}
test();
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
class Subject {
constructor() {
this.observers = [];
}
attach(observer) {
this.observers.push(observer);
}
notify(data) {
this.observers.forEach(obs => obs.update(data));
}
}
const subject = new Subject();
subject.attach({ update: (d) => console.log(d * 2) });
subject.attach({ update: (d) => console.log(d + 5) });
subject.notify(10);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const obj = { a: 1, b: 2, c: 3 };
Object.defineProperty(obj, 'd', {
value: 4,
enumerable: false
});
const entries = Object.entries(obj);
const keys = Object.keys(obj);
const values = Object.values(obj);
console.log(entries.length + keys.length + values.length);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
console.log(1);
setTimeout(() => {
console.log(2);
}, 100);
setTimeout(() => {
console.log(3);
}, 0);
Promise.resolve().then(() => {
console.log(4);
}).then(() => {
console.log(5);
});
console.log(6);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const target = { name: 'Maya', age: 25 };
const handler = {
get(obj, prop) {
if (prop in obj) {
return obj[prop];
}
return `Property '${prop}' not found`;
},
set(obj, prop, value) {
if (typeof value === 'string') {
obj[prop] = value.toUpperCase();
} else {
obj[prop] = value;
}
return true;
}
};
const proxy = new Proxy(target, handler);
proxy.city = 'tokyo';
console.log(proxy.name);
console.log(proxy.city);
console.log(proxy.country);
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
class Logger {
constructor(prefix) {
this.prefix = prefix;
}
log(message) {
console.log(`${this.prefix}: ${message}`);
}
}
class Database {
constructor(logger) {
this.logger = logger;
}
save(data) {
this.logger.log(`Saving ${data}`);
return `${data}_saved`;
}
}
const logger = new Logger('DB');
const db = new Database(logger);
const result = db.save('user');
console.log(result);
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
function Vehicle(type) {
this.type = type;
}
Vehicle.prototype.wheels = 4;
Vehicle.prototype.getInfo = function() {
return `${this.type}: ${this.wheels}`;
};
const car = new Vehicle('Car');
const bike = Object.create(car);
bike.type = 'Bike';
bike.wheels = 2;
console.log(car.getInfo() + ' | ' + bike.getInfo());
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const numbers = [1, 2, 3];
numbers.push(4);
console.log(numbers);
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const x = null;
const y = undefined;
const z = 0;
const result1 = x ?? 'default';
const result2 = y ?? 'default';
const result3 = z ?? 'default';
const combined = result1 ? result2 ? result3 : 'B' : 'C';
console.log(combined);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
async function processValues() {
try {
console.log('Start');
const a = await Promise.resolve('First');
console.log(a);
const b = await Promise.reject('Error');
console.log(b);
return 'Done';
} catch (err) {
console.log(err);
return 'Recovered';
} finally {
console.log('Finally');
}
}
processValues().then(result => console.log(result));
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
console.log(typeof null);
console.log(typeof undefined);
console.log(typeof {});
console.log(typeof []);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const a = [1, 2, 3];
const b = a.flatMap(x => [x, x * 2]);
console.log(b);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const json = '{"name":"Sarah","age":25,"active":true}';
const obj = JSON.parse(json);
obj.name = "Emma";
obj.age++;
const json2 = JSON.stringify(obj);
const obj2 = JSON.parse(json2);
obj.age = 100;
console.log(obj2.age);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const numbers = [1, 2, 3, 4, 5];
const pipeline = (...fns) => (value) => fns.reduce((acc, fn) => fn(acc), value);
const multiply = (x) => (y) => x * y;
const add = (x) => (y) => x + y;
const square = (x) => x * x;
const transform = pipeline(
multiply(2),
add(3),
square
);
const result = numbers.map(transform);
console.log(result);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const multiplier = 3;
function createCounter() {
let count = 0;
const multiplier = 5;
return function() {
count++;
return count * multiplier;
};
}
const counter = createCounter();
console.log(counter());
console.log(counter());
console.log(multiplier);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const clothes = [ 'jacket', 't-shirt' ];
clothes.length = 0;
console.log(clothes[0]);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const weakMap = new WeakMap();
const obj1 = {};
const obj2 = { key: 'value' };
weakMap.set(obj1, obj2);
const result = weakMap.get(obj1).key.split('').reverse().join('');
console.log(result);
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
for (var i =0; i < 10; i++){
setTimeout(function (){
console.log(i);
}, 0);
}
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
Выбирай направление:
Промпты, обучение, шпаргалки и полезные ресурсы на каждую тему!
Please open Telegram to view this post
VIEW IN TELEGRAM