TGViewer
Channel Public Channel
JavaScript

JavaScript

@javascript

A resourceful newsletter featuring the latest and most important news, articles, books and updates in the world of #javascript 🚀 Don't miss our Quizzes!

Let's chat: @nairihar
Subscribers
31.1K
Photos
1.2K
Videos
10
Links
895

Showing posts older than #2983 · Back to latest

Older Posts 20 shown
Post #2982 3.78K
CHALLENGE

class DataProcessor {
static #cache = new Map();
static #initialized = false;

static {
console.log('First block');
this.#cache.set('default', 'value1');
}

static {
console.log('Second block');
this.#cache.set('config', 'value2');
this.#initialized = true;
}

static getStatus() {
return `${this.#cache.size}-${this.#initialized}`;
}
}

console.log(DataProcessor.getStatus());
  • 👍 4
  • ❤ 3
Post #2980 3.57K
🌪 GitHub is exploring solutions to tackle low-quality contributions, which could include giving you the option to disable PRs entirely or at least restrict them to collaborators. Got opinions? Join the discussion.
  • 👍 7
  • ❤ 5
  • 🔥 4
Post #2979 3.72K
  • ❤ 5
  • 👍 1
Post #2978 3.63K
CHALLENGE

const target = { name: "Sarah", age: 25 };

const handler = {
get(obj, prop) {
if (prop === 'greeting') {
return `Hello, I'm ${obj.name}`;
}
return obj[prop]?.toString().toUpperCase() || 'UNKNOWN';
},

set(obj, prop, value) {
if (typeof value === 'string') {
obj[prop] = value.toLowerCase();
} else {
obj[prop] = value * 2;
}
return true;
}
};

const proxy = new Proxy(target, handler);
console.log(proxy.name);
proxy.city = "Boston";
proxy.score = 15;
console.log(proxy.greeting);
console.log(proxy.city + " " + proxy.score);
  • 👍 16
  • ❤ 6
  • 🔥 4
Post #2976 3.33K
  • ❤ 2
  • 👍 2
  • 🔥 1
Post #2975 3.42K
CHALLENGE

let counter = 0;

const increment = () => ++counter;
const getValue = () => counter;

const obj = {
get value() {
increment();
return getValue();
}
};

console.log(obj.value);
console.log(obj.value);
console.log(counter);
  • ❤ 8
  • 🔥 5
  • 👍 2
Post #2974 3.4K
⁉️ How Not to Parse Numbers in JavaScript

Why use a proper locale-aware API to parse numbers when you can hand-roll a maze of string splits, separator swaps, and implicit type coercions that silently break on edge cases?

Remy Porter (The Daily WTF)
  • ❤ 6
  • 🔥 6
  • 👍 3
Post #2973 3.16K
  • ❤ 2
  • 🔥 2
  • 👍 1
Post #2972 3.3K
CHALLENGE

const memoize = (fn) => {
const cache = new Map();
return (...args) => {
const key = JSON.stringify(args);
if (cache.has(key)) return cache.get(key);
const result = fn(...args);
cache.set(key, result);
return result;
};
};

const fibonacci = memoize((n) => {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
});

console.log(fibonacci(10));
console.log(fibonacci(5));
console.log(fibonacci(10));
  • 🔥 8
  • ❤ 4
  • 👍 2
Post #2971 3.08K
✌️ Four Heavyweights Drop Updates

Four stalwarts of the JavaScript ecosystem all shipped notable releases this week, and odds are you're using at least one of them:

• Gatsby v5.16 proves Gatsby, once considered neck-and-neck with Next.js in the React world, is not 'dead'. The headline feature is React 19 support.

• Babel 7 just shipped its final release. "After years in the making, Babel 8 is finally ready," in release candidate form, at least.

• Rspress 2.0 is a major release for the high-performance Rust-powered, but JavaScript-facing, static site generator.

• Lodash 4.17.23 sounds minor, but it's a 'security reset' for the still heavily used utility library and is designed to provide a base for a longer future.
  • 🔥 9
  • ❤ 5
  • 👍 5
Post #2970 2.86K
  • ❤ 5
  • 👍 3
  • 🔥 2
Post #2969 3.16K
CHALLENGE

const source = { a: 1, b: { c: 2 } };
const target1 = Object.assign({}, source);
const target2 = { ...source };

source.b.c = 99;
target1.a = 10;
target2.b.c = 50;

console.log(source.a);
console.log(source.b.c);
console.log(target1.a);
console.log(target1.b.c);
console.log(target2.a);
console.log(target2.b.c);
  • ❤ 6
  • 👍 4
  • 🔥 3
Post #2968 3.45K
  • 👍 6
  • ❤ 2
  • 🤣 2
  • 🔥 1
Post #2967 2.96K
CHALLENGE

const obj = {
name: 'Sarah',
greet() {
console.log(`Hello, ${this.name}`);
}
};

const person = { name: 'Mike' };
const func = obj.greet;

obj.greet();
func();
func.call(person);
person.hello = obj.greet;
person.hello();
  • 🔥 7
  • 👍 5
  • ❤ 4
Post #2966 3.43K
  • 👍 4
  • ❤ 1
  • 🔥 1
Post #2965 3.4K
CHALLENGE

const target = { name: 'Sarah', age: 25 };

const handler = {
get(obj, prop) {
if (prop === 'toString') {
return () => `Person: ${obj.name}`;
}
return Reflect.get(obj, prop);
},
set(obj, prop, value) {
if (prop === 'age' && value < 0) {
return false;
}
return Reflect.set(obj, prop, value);
}
};

const proxy = new Proxy(target, handler);
proxy.age = -5;
proxy.location = 'NYC';
console.log(proxy.toString());
console.log(proxy.age);
console.log(proxy.location);
  • ❤ 9
  • 👍 6
  • 🔥 2
Post #2964 3.14K
  • ❤ 6
  • 🤔 3
  • 👍 2
  • 🔥 1
Post #2963 3.36K
CHALLENGE

function processData() {
try {
console.log('start');
throw new Error('oops');
console.log('unreachable');
} catch (e) {
console.log('caught');
return 'error';
} finally {
console.log('cleanup');
}
console.log('end');
}

const result = processData();
console.log(result);
  • 🔥 9
  • ❤ 7
  • 👍 4
Post #2962 3.51K
  • ❤ 3
  • 👍 1
  • 🔥 1
Older posts →
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →