Click here to Skip to main content
15,889,216 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralClosures (C#) Pin
Marc Clifton4-Mar-24 3:56
mvaMarc Clifton4-Mar-24 3:56 
GeneralRe: Closures (C#) Pin
Richard Deeming4-Mar-24 4:20
mveRichard Deeming4-Mar-24 4:20 
GeneralRe: Closures (C#) Pin
Marc Clifton4-Mar-24 7:49
mvaMarc Clifton4-Mar-24 7:49 
JokeRe: Closures (C#) Pin
0x01AA4-Mar-24 6:05
mve0x01AA4-Mar-24 6:05 
GeneralRe: Closures (C#) Pin
Sander Rossel4-Mar-24 8:59
professionalSander Rossel4-Mar-24 8:59 
GeneralRe: Closures (C#) Pin
jmaida4-Mar-24 12:29
jmaida4-Mar-24 12:29 
GeneralRe: Closures (C#) Pin
Stacy Dudovitz5-Mar-24 17:31
professionalStacy Dudovitz5-Mar-24 17:31 
GeneralRe: Closures (C#) Pin
Jeremy Falcon4-Mar-24 12:50
professionalJeremy Falcon4-Mar-24 12:50 
Marc Clifton wrote:
I think this would make a good interview question!
To mention the language that shall not be named (it rhymes with GuavaScript), closures was one of the ways to fake OOP before es6 came along with classes.
JavaScript
function FakeClass() {
  // analogous to a protected variable
  const protectMe = 'When I grow up, I wanna be a class!';  

    // with an anonymous function (kinda like a method)
  this.doSomething = function() {
    // "protectMe" will exist after this function is called
    console.log(protectMe);
  }
}

const sup = new FakeClass();

console.log(sup.protectMe); // not defined
sup.doSomething(); // prints "When I grow up, I wanna be a class!"

These days I tend to use closures less and less with generators, promises, async/await, etc. There are just new ways to do the same thing in the language that shall not name be named. But, if you ever find yourself needing to access that lexical context after the parent function go bye bye (a static variable wouldn't quite cut it for that) and you don't want to clutter up your code with global/module crap... closures are a life saver.

All-in-all, cool stuff.
Jeremy Falcon

GeneralRe: Closures (C#) Pin
jmaida4-Mar-24 15:00
jmaida4-Mar-24 15:00 
GeneralRe: Closures (C#) Pin
Jeremy Falcon4-Mar-24 17:53
professionalJeremy Falcon4-Mar-24 17:53 
GeneralRe: Closures (C#) Pin
jmaida4-Mar-24 15:41
jmaida4-Mar-24 15:41 
GeneralRe: Closures (C#) Pin
Jeremy Falcon4-Mar-24 17:54
professionalJeremy Falcon4-Mar-24 17:54 
GeneralRe: Closures (C#) Pin
Jacquers4-Mar-24 17:43
Jacquers4-Mar-24 17:43 
GeneralRe: Closures (C#) Pin
englebart4-Mar-24 14:40
professionalenglebart4-Mar-24 14:40 
GeneralRe: Closures (C#) Pin
Peter Adam4-Mar-24 20:20
professionalPeter Adam4-Mar-24 20:20 
GeneralRe: Closures (C#) Pin
Fly Gheorghe4-Mar-24 21:36
Fly Gheorghe4-Mar-24 21:36 
GeneralRe: Closures (C#) Pin
RooN3y5-Mar-24 2:18
RooN3y5-Mar-24 2:18 
GeneralRe: Closures (C#) Pin
Rich Shealer5-Mar-24 3:14
Rich Shealer5-Mar-24 3:14 
GeneralA Mouse Ring Pin
BernardIE53174-Mar-24 0:43
BernardIE53174-Mar-24 0:43 
GeneralRe: A Mouse Ring Pin
Kornfeld Eliyahu Peter4-Mar-24 1:05
professionalKornfeld Eliyahu Peter4-Mar-24 1:05 
GeneralRe: A Mouse Ring Pin
den2k884-Mar-24 3:17
professionalden2k884-Mar-24 3:17 
GeneralRe: A Mouse Ring Pin
dandy724-Mar-24 3:20
dandy724-Mar-24 3:20 
GeneralRe: A Mouse Ring Pin
Kornfeld Eliyahu Peter4-Mar-24 17:50
professionalKornfeld Eliyahu Peter4-Mar-24 17:50 
GeneralRe: A Mouse Ring Pin
mspad5-Mar-24 3:58
mspad5-Mar-24 3:58 
GeneralRe: A Mouse Ring Pin
dandy725-Mar-24 4:29
dandy725-Mar-24 4:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.