Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. JavaScript closures...

JavaScript closures...

Scheduled Pinned Locked Moved The Lounge
javascripthelp
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeremy Falcon
    wrote on last edited by
    #1

    Since I just ran across yet another person who's technically brilliant (in other areas) but doesn't understand what a closure is in JavaScript... And it probably doesn't help that most of the JS articles give lousy explanations (because they don't know it themselves most likely). In plain English, a closure is not just a function inside a function. That's a nested function. A closure _retains_ its lexical state (variables) after the parent function has exited. Now, technically you can create a closures and never leverage this, in which case it'll just look like a function in a function. Which is where I assume why some think that's what a closure is. But, putting a function in a function in another language is not a closure unless there is retention. Exhibit A:

    function bro() {
    let dude = 'Howdy Partner';

    return function bruh() {
    console.info(dude);
    }();
    }

    // prints Howdy Partner
    bro().bruh;

    I'm convinced the only reason JS is considered hard is the sheer amount of FUD out there by peeps who don't study it and use terms from it to sound cool. So, you're welcome. :laugh:

    Jeremy Falcon

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups