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. Just because I'm tired of seeing people say it can't be done...

Just because I'm tired of seeing people say it can't be done...

Scheduled Pinned Locked Moved The Lounge
javascriptc++question
10 Posts 6 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

    Yes, you can have a static variable in a named function in JavaScript/TypeScript. Even peeps on SO say the only way to achieve this is with a class. Nope. Exhibit A:

    // this must be a named function
    function datNice(): number {
    const self = datNice as any;
    const meSoStatic = self.meSoStatic ? self.meSoStatic : 42;

    // this is the only difference between C/C++ since there's no static keyword
    // we gotta do this assignment for the juju
    self.meSoStatic = meSoStatic;

    return meSoStatic;
    }

    console.log(datNice()); // 42
    console.log(datNice()); // 42

    Note: You used to able to pull this off with an anonymous function using arguments.callee, but that's since been deprecated, due to misuse or the confusion around this I can only assume. But, named functions still work perfectly.

    Jeremy Falcon

    M A C 4 Replies Last reply
    0
    • J Jeremy Falcon

      Yes, you can have a static variable in a named function in JavaScript/TypeScript. Even peeps on SO say the only way to achieve this is with a class. Nope. Exhibit A:

      // this must be a named function
      function datNice(): number {
      const self = datNice as any;
      const meSoStatic = self.meSoStatic ? self.meSoStatic : 42;

      // this is the only difference between C/C++ since there's no static keyword
      // we gotta do this assignment for the juju
      self.meSoStatic = meSoStatic;

      return meSoStatic;
      }

      console.log(datNice()); // 42
      console.log(datNice()); // 42

      Note: You used to able to pull this off with an anonymous function using arguments.callee, but that's since been deprecated, due to misuse or the confusion around this I can only assume. But, named functions still work perfectly.

      Jeremy Falcon

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #2

      So essentially, you've extended the function with a static variable? If I understand this correctly?

      Latest Articles:
      A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework

      J E 3 Replies Last reply
      0
      • J Jeremy Falcon

        Yes, you can have a static variable in a named function in JavaScript/TypeScript. Even peeps on SO say the only way to achieve this is with a class. Nope. Exhibit A:

        // this must be a named function
        function datNice(): number {
        const self = datNice as any;
        const meSoStatic = self.meSoStatic ? self.meSoStatic : 42;

        // this is the only difference between C/C++ since there's no static keyword
        // we gotta do this assignment for the juju
        self.meSoStatic = meSoStatic;

        return meSoStatic;
        }

        console.log(datNice()); // 42
        console.log(datNice()); // 42

        Note: You used to able to pull this off with an anonymous function using arguments.callee, but that's since been deprecated, due to misuse or the confusion around this I can only assume. But, named functions still work perfectly.

        Jeremy Falcon

        M Offline
        M Offline
        Marc Clifton
        wrote on last edited by
        #3

        BTW, when I run your code in the Edge console, I get: VM301:1 Uncaught SyntaxError: Unexpected token ':' I assume on the line: const meSoStatic = self.meSoStatic ? self.meSoStatic : 42; Same error in Chrome.

        Latest Articles:
        A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework

        1 Reply Last reply
        0
        • M Marc Clifton

          So essentially, you've extended the function with a static variable? If I understand this correctly?

          Latest Articles:
          A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework

          J Offline
          J Offline
          Jeremy Falcon
          wrote on last edited by
          #4

          Yup. In functional languages, functions are first-class citizens. Which is just a fancy way of saying they can be treated like any other data type. They have a `this` and are also self referential.

          Jeremy Falcon

          1 Reply Last reply
          0
          • M Marc Clifton

            So essentially, you've extended the function with a static variable? If I understand this correctly?

            Latest Articles:
            A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework

            J Offline
            J Offline
            Jeremy Falcon
            wrote on last edited by
            #5

            Oh, about the syntax error thingy... it's TypeScript. There's a cool site, kinda like jsfiddle, if you just wanna quickly play around with example code for TS: [https://www.typescriptlang.org/play\](https://www.typescriptlang.org/play). Or [jsfiddle](https://jsfiddle.net/) works if you select the TS language.

            Jeremy Falcon

            1 Reply Last reply
            0
            • M Marc Clifton

              So essentially, you've extended the function with a static variable? If I understand this correctly?

              Latest Articles:
              A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework

              E Offline
              E Offline
              englebart
              wrote on last edited by
              #6

              To my mind, this is really an instance variable on a global function. Since all code will use the same instance of the global function, all code will reuse its single instance variable.

              1 Reply Last reply
              0
              • J Jeremy Falcon

                Yes, you can have a static variable in a named function in JavaScript/TypeScript. Even peeps on SO say the only way to achieve this is with a class. Nope. Exhibit A:

                // this must be a named function
                function datNice(): number {
                const self = datNice as any;
                const meSoStatic = self.meSoStatic ? self.meSoStatic : 42;

                // this is the only difference between C/C++ since there's no static keyword
                // we gotta do this assignment for the juju
                self.meSoStatic = meSoStatic;

                return meSoStatic;
                }

                console.log(datNice()); // 42
                console.log(datNice()); // 42

                Note: You used to able to pull this off with an anonymous function using arguments.callee, but that's since been deprecated, due to misuse or the confusion around this I can only assume. But, named functions still work perfectly.

                Jeremy Falcon

                A Offline
                A Offline
                Andre Oosthuizen
                wrote on last edited by
                #7

                It is all frowned upon just because some "clever beaver" on Google stated so, like sheep, lets follow the rest... I agree 300%, all si possible, how did we get to the "clever" parts, because we used to use 'THIS' first and then tried to make it into rocket science. Blows my mind why we would consistently keep on making things difficult, 1+1=2 point, why try to do 0.8+1.2=2, my 5 cents and upvote! :^)

                J 1 Reply Last reply
                0
                • A Andre Oosthuizen

                  It is all frowned upon just because some "clever beaver" on Google stated so, like sheep, lets follow the rest... I agree 300%, all si possible, how did we get to the "clever" parts, because we used to use 'THIS' first and then tried to make it into rocket science. Blows my mind why we would consistently keep on making things difficult, 1+1=2 point, why try to do 0.8+1.2=2, my 5 cents and upvote! :^)

                  J Offline
                  J Offline
                  jochance
                  wrote on last edited by
                  #8

                  JS is like mummyScript... it's made of bandages that will never fix the inherent problem of being dead at the core.

                  1 Reply Last reply
                  0
                  • J Jeremy Falcon

                    Yes, you can have a static variable in a named function in JavaScript/TypeScript. Even peeps on SO say the only way to achieve this is with a class. Nope. Exhibit A:

                    // this must be a named function
                    function datNice(): number {
                    const self = datNice as any;
                    const meSoStatic = self.meSoStatic ? self.meSoStatic : 42;

                    // this is the only difference between C/C++ since there's no static keyword
                    // we gotta do this assignment for the juju
                    self.meSoStatic = meSoStatic;

                    return meSoStatic;
                    }

                    console.log(datNice()); // 42
                    console.log(datNice()); // 42

                    Note: You used to able to pull this off with an anonymous function using arguments.callee, but that's since been deprecated, due to misuse or the confusion around this I can only assume. But, named functions still work perfectly.

                    Jeremy Falcon

                    C Offline
                    C Offline
                    charlieg
                    wrote on last edited by
                    #9

                    play with me why is this even relevant in code? I see so much cute, clever, whiz kid bull$$hit it makes my eyes water. I won't miss it.

                    Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.

                    J 1 Reply Last reply
                    0
                    • C charlieg

                      play with me why is this even relevant in code? I see so much cute, clever, whiz kid bull$$hit it makes my eyes water. I won't miss it.

                      Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.

                      J Offline
                      J Offline
                      Jeremy Falcon
                      wrote on last edited by
                      #10

                      Because sometimes you want persistence without cluttering up the global/module namespace. Makes it pretty easy where to track down what in a larger project. Should it be overdone? Nope. But that goes with anything.

                      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