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
899

Showing posts older than #2375 Β· Back to latest

Older Posts 20 shown
Post #2374 4.55K
😭 php-node: A New Way to Bring PHP and Node Together

I bet some readers have strong feelings about the idea of mixing PHP and Node.js, but this is a neat project. php-node is a native module for Node that enables the running of PHP apps within the Node environment. Why? For migrating legacy apps, building hybrid PHP/JS apps, or Node apps that simply need to call out to PHP for some reason (WordPress, maybe, as we see in this post).

Matteo Collina et al.
  • πŸ‘ 6
  • πŸ€” 4
  • πŸ”₯ 3
  • ❀ 2
Post #2373 4.18K
  • πŸ‘ 5
  • πŸ”₯ 4
  • ❀ 1
Post #2372 4.66K
CHALLENGE

function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(this, args);
}
return function(...moreArgs) {
return curried.apply(this, [...args, ...moreArgs]);
};
};
}

const multiply = curry((a, b, c) => a * b * c);
const double = multiply(2);
const result = double(3)(4);

console.log(result);
  • πŸ”₯ 3
Post #2371 4.59K
  • πŸ‘ 6
  • πŸ”₯ 3
  • ❀ 1
Post #2370 4.46K
CHALLENGE

const target = { a: 1, b: 2 };
const handler = {
get(obj, prop) {
return prop in obj ? obj[prop] * 2 : 'not found';
},
set(obj, prop, value) {
if (typeof value !== 'number') {
return false;
}
obj[prop] = value + 10;
return true;
}
};

const proxy = new Proxy(target, handler);
proxy.c = '5';
proxy.d = 5;

console.log(JSON.stringify({
a: proxy.a,
b: proxy.b,
c: proxy.c,
d: target.d,
hasC: Reflect.has(target, 'c')
}));
  • πŸ€” 6
  • ❀ 1
  • πŸ”₯ 1
Post #2368 4.8K
  • πŸ‘ 6
  • ❀ 5
  • πŸ€” 2
  • 🀣 2
Post #2367 4.67K
CHALLENGE

const team = {
lead: { name: 'Alice', projects: ['Alpha', 'Beta'] },
dev: { name: 'Bob', projects: ['Gamma'] },
tester: { name: 'Charlie', projects: [] }
};

const {
lead: { projects: [leadProject] },
dev: { projects: [devProject = 'Delta'] },
tester: { projects: [testerProject = 'Epsilon'] }
} = team;

console.log(`${leadProject}-${devProject}-${testerProject}`);
  • ❀ 3
Post #2366 4.53K
🀩 Wake Up, Remix! Everything's Changing..

Big news from the Remix camp this week. About a year ago, Remix and React Router merged together reflecting their shared goals and code, but now it’s all change again. React Router is now basically what Remix originally intended to be, and so β€˜Remix’ is rebooting as a model-first, low-dependency, Web API-centric full-stack framework built on Preact. It’ll no longer be a 'React framework' per se.

Michael Jackson and Ryan Florence
  • ❀ 5
  • πŸ‘ 1
  • πŸ”₯ 1
Post #2365 4.32K
  • πŸ‘ 7
Post #2364 4.47K
CHALLENGE

class ShoppingCart {
constructor() {
if (ShoppingCart.instance) {
return ShoppingCart.instance;
}

this.items = [];
ShoppingCart.instance = this;
}

addItem(item) {
this.items.push(item);
}

getItems() {
return [...this.items];
}
}

const cart1 = new ShoppingCart();
const cart2 = new ShoppingCart();

cart1.addItem('Book');
cart2.addItem('Laptop');

console.log(cart1.getItems());
  • ❀ 3
Post #2363 4.31K
😭 Matteo Collina has unveiled php-node, a way to run PHP apps within the same process as Node.js. Run WordPress with a Next.js frontend? Sure.
  • πŸ€” 11
  • 🀣 8
  • πŸ‘ 4
Post #2362 4.76K
  • ❀ 4
  • πŸ”₯ 1
Post #2361 4.49K
CHALLENGE

const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' }
];

const userScores = new WeakMap();

// Set scores for users
userScores.set(users[0], 95);
userScores.set(users[1], 80);

// Remove reference to Bob
users[1] = null;

let sum = 0;
for (const user of users) {
if (user && userScores.has(user)) {
sum += userScores.get(user);
}
}

console.log(sum);
  • πŸ‘ 7
  • ❀ 2
Post #2360 4.59K
  • πŸ‘ 8
  • ❀ 2
Post #2359 4.64K
CHALLENGE

function* counter() {
let i = 1;
while (true) {
const reset = yield i++;
if (reset) {
i = 1;
}
}
}

const gen = counter();
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next(true).value);
console.log(gen.next().value);
  • ❀ 2
Post #2358 4.91K
  • ❀ 3
  • πŸ‘ 3
  • πŸ”₯ 2
Post #2357 4.89K
CHALLENGE

try {
try {
// Creating a custom error type
class CustomError extends Error {
constructor(message) {
super(message);
this.name = "CustomError";
}
}

const fn = () => {
throw new CustomError("Something went wrong");
};

try {
fn();
} catch (e) {
throw new TypeError("Type error occurred");
}
} catch (e) {
console.log(e instanceof Error, e.name, e instanceof TypeError);
}
} catch (e) {
console.log("Outer catch", e.message);
}
  • ❀ 5
  • πŸ‘ 2
  • πŸ”₯ 1
Post #2356 4.6K
  • πŸ‘ 8
  • πŸ€” 4
  • ❀ 2
  • πŸ”₯ 2
Post #2355 4.79K
CHALLENGE

function compose(...funcs) {
return function(x) {
return funcs.reduceRight((acc, fn) => fn(acc), x);
};
}

const add10 = x => x + 10;
const multiply2 = x => x * 2;
const toString = x => `Result: ${x}`;

const composed = compose(toString, multiply2, add10);
console.log(composed(5));
  • πŸ‘ 6
  • ❀ 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 β†’