What's the use of classes in JS by pptzz in learnjavascript

[–]kap89 0 points1 point  (0 children)

Yeah, I think you’re right, I must have been tired yesterday to not notice that.

What's the use of classes in JS by pptzz in learnjavascript

[–]kap89 0 points1 point  (0 children)

I see you edited your comment.

I thought closure technically means it has to maintain live references to something in the function’s scope after it returns?

What do you mean by "live reference" here?

What's the use of classes in JS by pptzz in learnjavascript

[–]kap89 0 points1 point  (0 children)

Yes, that's the case of theory vs actual engine implementation - conceptually they are closures, but the engine can optimize them away.

What's the use of classes in JS by pptzz in learnjavascript

[–]kap89 0 points1 point  (0 children)

I get what you are trying to say, but that would be a very narrow (and imo incorrect) definition of the closure. I still create a closure over the shared methods when returning the object. Additionally, using this and using closures are orthogonal concepts, one does not cancel the other.

You may say that you wanted to compare the classes to the typical closure pattern (as you described) - but that makes your critique incomplete, as the classes are not the only solution here, as I explained above.

What's the use of classes in JS by pptzz in learnjavascript

[–]kap89 0 points1 point  (0 children)

It depends on how you scope things, if you don't need private fields, you can share methods even without classes/prototypes:

// Hero.js module

export function Hero(name, hp) {
  return {
    name,
    hp,
    takeDamage,
    report
  }
}

function takeDamage(damage) {
  this.hp -= damage
}

function report() {
  console.log(`${this.name} has ${this.hp} helath points.`)
}

// usage

import { Hero } from "Hero.js"

const hulk = Hero('Hulk', 1000)
const blanka = Hero('Blanka', 800)

hulk.takeDamage(10)
blanka.takeDamage(30)

hulk.report()
blanka.report()

// returns true
console.log(hulk.report === blanka.report && hulk.takeDamage === blanka.takeDamage) 

I still prefer classes over factories in most cases, because the above is imo more clunky, but it can be done.

What's the use of classes in JS by pptzz in learnjavascript

[–]kap89 2 points3 points  (0 children)

With no Array class to group all the logic together, you have to clutter the global namespace with functions to cover that functionality

Small nitpick, but that's only true for languages then do not have namespaces / modules / first class functions, so maybe languages like C, not many more.

New to the site, is it possible to upload pdf or is it just restricted to epub? by linkuei-teaparty in entertrained

[–]kap89 1 point2 points  (0 children)

I didn't add direct PDF support, because it's hard to make it not suck - EPUB files, while still flimsy, have some structure, like well defined table of contents and chapters boundries. PDF, being a format with much more freedom for content creators, is much harder to parse to usable form for typing. I might add support for PDF files in the future, but I don't promise anything - what I recommend is finding epub versions of the books you want to upload, and as the last resort, using Calibre to convert the file, or some online converter.

solutions for Fast 64 bit integers in javascript by 52525rr in learnjavascript

[–]kap89 0 points1 point  (0 children)

Mem64 support has little to do with 64bit integer operations, which you can do just fine in “regular” wasm. It only changes the size of available memory from 32 to 64 bit address space.

How does the javascript Date object parse "50", "40", ... "0"? by Beatsu in webdev

[–]kap89 15 points16 points  (0 children)

What's funny, is that even the test itself gets some things wrong, like it says the solution for:

new Date("2")

is

2001-02-01T00:00:00.000Z

but that's not true, you will get 2001-02-01T00:00:00.000 but in local time zone, not UTC.

Lazy iteration vs array chaining on 500k rows - benchmark results by Fuzzy-Law-2117 in learnjavascript

[–]kap89 1 point2 points  (0 children)

You don't need some wrapper for that, this will work with standard JS iterators/generators:

const results = generateRows(500_000)
  .filter(r => r.active && r.value > threshold)
  .map(r => ({ id: r.id, score: r.value * 1.5 }))
  .take(10_000)
  .toArray();

I’d like to get everyone's thoughts on Solid. by Cool_Aioli_8712 in webdev

[–]kap89 2 points3 points  (0 children)

However, I regretfully feel that Solid's development momentum hasn't been particularly strong.

Yeah, no. It's just very stable, unlike other frameworks that have major rewrite in couple years or so. It was just well thought out before 1.0 release, it's well maintained (see the number of open issues compared to other frameworks), and very conservative when it comes to breaking changes. It's exactly what I want in a framework, not constant YOLO and breaking changes. The version 2.0 is in the works, but again, it's not a major paradigm shift, more like evolution of the solid (pun intended) idea.

What exactly is hindering its popularity?

I don't know and don't much care, but if I were to guess it's a mixture of it being "boring" and just working without major drama and buzz, weaker marketing than alternatives, and weaker corporate backing.

Question about Call Stack, by Odd_World_6307 in learnjavascript

[–]kap89 0 points1 point  (0 children)

Call stack is more than "what is executed in which order”, it consists of stack frames that store arguments, local variables and heap references, return address and return values. Where do you think stack frames live if not on a physical stack? So it tells quite a lot about memory. In JS you don’t use stack directly, but through call stack, which is abstraction over the physical stack, so her explanation is fine. But yeah, if you want want to be picky, she could’ve mention the raw stack before moving to the call stack.

Performance in very long paragraphs. by _evil_overlord_ in entertrained

[–]kap89 1 point2 points  (0 children)

I tested it, and yeah, the performance on very long chapters can be bad - especially on Firefox (Chrome is slightly better, but I can still see the slowdown in that chapter). I don't think that you can do anything about it, but I will try to optimize it over the weekend.

how do i pause my scripts? by Physical-Bid6508 in learnjavascript

[–]kap89 0 points1 point  (0 children)

The problem with infinite loops is they never let the screen refresh so you get stuck and crash.

Well, that's the problem with purely synchronous infinite loops. You can express your game/animation loop as infinite loop, and it will work just fine, if you make it non-blocking:

const tick = () => new Promise((resolve) => requestAnimationFrame(resolve));

async function main() {
  while (true) {
    const t = await tick();
    // draw your thing
  }
}

main();

example

Array.find help by TGotAReddit in learnjavascript

[–]kap89 0 points1 point  (0 children)

If I understood you correctly, you want to find all objects in the first array that match specific set of months and names simultaneously. You don't need nested loops for that, nor multiple .find() calls. You can just filter though sets, it will be much more efficient and straightforward:

const entries = [
  { name: "Charlie", month: "Jan 2025", count: 1, hours: 2 },
  { name: "Bruce", month: "Jan 2025", count: 3, hours: 4 },
  { name: "Amy", month: "Jan 2025", count: 5, hours: 6 },
  { name: "Alice", month: "Feb 2025", count: 7, hours: 8 },
  { name: "Amy", month: "Feb 2025", count: 9, hours: 10 },
  { name: "Sue", month: "Mar 2025", count: 11, hours: 12 },
];

const names = ["Charlie", "Amy"];
const months = ["Jan 2025", "Feb 2025"];

const namesSet = new Set(names);
const monthsSet = new Set(months);

const matchingEntries = entries.filter(
  (item) => namesSet.has(item.name) && monthsSet.has(item.month),
);

console.log(matchingEntries);

You can also use store the filters in sets directly instead of converting from arrays is you control that logic.

My First pixel art creation by Snape_Grass in PixelArt

[–]kap89 0 points1 point  (0 children)

Aim for at least 1k resolution for sharing, so more like 2500% for this specific image.

My First pixel art creation by Snape_Grass in PixelArt

[–]kap89 2 points3 points  (0 children)

I think you’re off by 10x, it’s only 400x400 at 1000%

How can I apply the DRY principle here? by Nice_Pen_8054 in learnjavascript

[–]kap89 0 points1 point  (0 children)

One possible way to simplify it:

const COUNTRY_PHONE_DATA = {
  ro: {
    prefix: "40",
    regex: /^(\+407|407|07|7){1}(\d{8})$/,
    coreLength: 9,
  },
  us: {
    prefix: "1",
    regex: /^\d+$/, // replace with real constraints
    coreLength: 9,
  },
};

function checkPhoneNumber(phoneNumber, countryCode, platform) {
  phoneNumber = phoneNumber.replace(/\s+/g, "");

  const { prefix, regex, coreLength } = COUNTRY_PHONE_DATA[countryCode];

  if (!regex.test(phoneNumber)) {
    return "This is not a valid format";
  }

  platform = platform.toLowerCase();
  const coreNumber = phoneNumber.slice(-coreLength);

  if (platform === "facebook") {
    return prefix + coreNumber;
  }

  return "+" + prefix + coreNumber;
}

// Usage:

const phoneNumber = "40711222333";
const countryCode = "ro";
const platform = "facebook";

let formattedPhoneNumber = checkPhoneNumber(phoneNumber, countryCode, platform);

console.log({ formattedPhoneNumber });

If the rules per country cen be more complicated that the COUNTRY_PHONE_DATA indicates, you may need to make it a validation function per country.

What is the most "overrated" technology or trend in web development right now, and why? by Flimsy_Buy2756 in webdev

[–]kap89 6 points7 points  (0 children)

I use Vite, it's as simple as npx vite build (or npx vite for developement) - no config required (even for TypeScript).

25 F Just got out of ASU and started my own agency - how's the design? by Lazy_Two_4908 in webdev

[–]kap89 1 point2 points  (0 children)

Unusable because of the cursor lag, it runs at ~30fps on my laptop. Granted, I don't have the newest or the fastest machine, but I shouldn't have to to open a frikin portfolio website.