you are viewing a single comment's thread
view the rest of the comments
[–] 14 points 8 months ago* (13 children)

I get that this is a joke, but would something like import Math.random as getRandom work better? Because that basically what you're doing here, renaming the function.

  • source
  • hideshow 13 child comments
  • [–] 19 points 8 months ago (10 children)

    that's not really a thing in JS as Math isn't imported, it's just an object available globally. the closest you can get is like const { random: getRandom } = Mathbut that's just uglier.

    the implication is that this function is exported from a library so they have to keep the function around - obviously in a modern project you'd just do Math.random()

  • source
  • parent
  • hideshow 10 child comments
  • [–] 13 points 8 months ago (8 children)

    And that's only one of the many reasons that JavaScript is a clown language.

  • source
  • parent
  • hideshow 8 child comments
  • [–] 4 points 8 months ago (7 children)

    Which of these things, excactly? That Math just floats around in the global scope? Or that that destructuring assignment works? Or that the author chose to abstract around Math.random(). That has come very handy for me when testing.

  • source
  • parent
  • hideshow 7 child comments
  • [–] 8 points 8 months ago* (6 children)

    Global scope.

    Also that there's, after 35 years of webbrowsers, still no reliable way to match a domain without subdomain, except via string splitting.

  • source
  • parent
  • hideshow 6 child comments
  • [–] 6 points 8 months ago (4 children)

    I see your point regarding global scope. But personally, I’ve never encountered an issue with it. And it’s kinda nice not to have to import fetch every time you need it.

    Regarding subdomains, if you’ll humor my curiosity: What’s the use case? I also wonder what an API for this might look like.

    const {domain, subdomains, rootDomain} = new URL('https://wikipedia.org/')
    //       'wikipedia.org', [], 'wikipedia.org'
    const {domain, subdomains, rootDomain} = new URL('https://foo.bar.baz.net/')
    //       'foo.bar.baz.net', ['foo.bar.baz.net', 'bar.baz.net'], 'baz.net'
    
  • source
  • parent
  • hideshow 4 child comments
  • [–] 4 points 8 months ago* (last edited 8 months ago) (3 children)

    A userscript over links for debugging purposes, that should ignore links to the same domain (www.w3schools vs campus.w3schools vs www.youtube).

    Btw, it's funny how support.mozilla and some standards-teaching-sites are some of the worst offenders of web standards.

    Edit: how to stop this auto-linking?

  • source
  • parent
  • hideshow 3 child comments
  • [–] 2 points 8 months ago (2 children)

    If I understand correctly, you want to check the current domain (eg. w3schools) against api.w3schools and www.youtube, and return true for the first and false for the second (or the other way around)

    Then technically it’s possible without string splitting:

    const href = 'retrieved-from-the-anchor-element';
    (new URL(href, location.href).hostname).endsWith(location.hostname)
    
  • source
  • parent
  • hideshow 2 child comments
  • [–] 2 points 8 months ago*

    Most scripting languages have a global scope of some kind. It's not that big of a deal.

    I'd prefer not to have it, but it hasn't caused me problems in many years. Actually, the window object can be really useful in some situations so I'm not even sure about that.

  • source
  • parent
  • [–] 0 points 8 months ago (1 child)

    @abbadon420@sh.itjust.works @not_IO@lemmy.blahaj.zone @programmer_humor@programming.devI didn't know about this specific syntax you mentioned (import foo.bar as baz; what I'm aware and I use frequently is something like e.g. const log = console.log.bind(console)), I'm not even sure if it works as all my import use cases involve something installed from NPM or a relative-folder module file.But sometimes it's useful, and better, to have parametrized randomness, as in my helper functions I keep reusing across my personal projects:export const rand = (n, x) => Math.random() * (x - n) + n export const irand = (n, x) => Math.round(rand(n, x)) export const choose = a => a[irand(0, a.length-1)](yeah, my choose helper lacks a proper verification of the input parameter, will return undefined if the array is empty or is not an array, but, well, it's for personal projects so I don't really worry about that; also I've been using Ruby more than I use JS, and Ruby got beautifully native array.sample and Random.rand(a..b))

  • source
  • parent
  • hideshow 1 child comment
  • [–] 1 point 8 months ago*

    I think irand should floor instead of round. First, when you want to generate a random number between 0 and 6 it’s often useful to exclude the 6. (See also https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html).

    Second, irand(0, 6) has a higher chance of rolling a 1 than a 0 because the capture area for 1 is [.5, 1.5), while the capture area for 0 is only [0, .5)

    Also if you had chosen any other variable names, it wouldn’t have taken me 5 minutes to figure out what these things do.

    And one more, if you had swapped the input parameters, and defaulted to n=0, you could call the use case “random number up to 6” with just rand(6) instead of rand(0, 6).

    None of that matters in a private project, but… it just itched to point these things out.

  • source
  • parent