Three Techniques for Avoiding Global Variables in JavaScript

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ก.ย. 2024

ความคิดเห็น • 47

  • @iben1195
    @iben1195 2 ปีที่แล้ว +1

    I coded a word base game but it wasn't light on cpu. Why was it so slow, it baffled me all the while. On research, I learned of memory leaks and i was sure my game had those in abundant. Now how do I minimize global declarations? 5 yrs later, your video is still top notch as you demonstrate with utmost clarity. Gracias, shukran, merci, thank you 🙏

  • @oaakinola1234
    @oaakinola1234 5 ปีที่แล้ว +3

    Two words mate, Life and Saver! I'd happily donate some funds if I knew how to?

  • @RamKumar-rg3gr
    @RamKumar-rg3gr 4 ปีที่แล้ว

    Sir u are best JavaScript teacher ever

  • @ayushkushwaha171
    @ayushkushwaha171 ปีที่แล้ว

    Good explanation. I found your channel while preparing for my interviews last month and you videos are really helping me a lot. Thanks for these and keep doing it :)

  • @raysun5889
    @raysun5889 5 ปีที่แล้ว +2

    Thank you sir, it's very much helpful.

  • @benzflynn
    @benzflynn 5 ปีที่แล้ว

    A great follow-up video to *What Is Wrong With Global Variables In JavaScript ?*
    Explanations clear and down-to-earth. Many thanks, Buddy.

  • @rajeevrevada115
    @rajeevrevada115 3 ปีที่แล้ว

    Still the best video series on JS. Thanks a lot. Love from India ❤️

  • @joshp0093
    @joshp0093 5 ปีที่แล้ว

    Fantastic video. You made everything very clear and easy to understand. Thanks for explaining this in great detail.

  • @Shulkerkiste
    @Shulkerkiste 4 ปีที่แล้ว +2

    Holy Shit!
    This is the perfekt video I needed!
    Thank you so much

  • @MrJdm222
    @MrJdm222 3 ปีที่แล้ว

    thanks

  • @filiplahoda248
    @filiplahoda248 4 ปีที่แล้ว

    Nice videos. Good improvement would be to sometimes mention why does it work like that. For example if we put a function declaration into parentheses, what does it mean in the language of JS

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว

      This tutorial on IIFEs explains that better: th-cam.com/video/w71IVgfsOL0/w-d-xo.html

  • @leegold7100
    @leegold7100 6 ปีที่แล้ว +1

    Is source code available? It's totally understandable if it's only available through your courses...Just asking. Thanks for sharing your knowledge. These videos are great !

    • @AllThingsJavaScript
      @AllThingsJavaScript  6 ปีที่แล้ว

      These same topics are covered in the courses that provide the source code. I haven't made the code available for the TH-cam tutorials, but something to consider. Thanks for the suggestion.

    • @misterhtmlcss
      @misterhtmlcss 6 ปีที่แล้ว +1

      I was just looking for that too. It's common to allow access to the code, because that way I can open it and watch and refer back e.g. pause, review, think, probably think some more and go back to the video.
      I get you have courses, so I guess it makes sense to limit the value some how to encourage subs on udemy or any other place. The only thing is, is that if that's the true value difference I personally wouldn't choose to sign up for access to source code. Maybe others would, but I thought I'd at least share my disposition on the point. FYI. My little sidebar is not a criticism of your choice, just me sharing what my mental process is on it as one person. I give feedback whenever I can, because providers rarely get enough and so I try to share whenever I like someone. For better or worse ;)
      PS. I've watched quite a few and I really like your work; very unique teaching approach which I like very much.

    • @benzflynn
      @benzflynn 5 ปีที่แล้ว

      Similar view to misterhtmlcss on your putting code examples on the web for each significant video.
      I also question the wisdom of denying access to the - usually low-level, non-ground-shaking - source code for far of suppressing demand for your courses. If anything, seeing a thing work might help to DRIVE demand for the full course ! Especially if the examples were close to the issues frontend coders have to deal with as they learn the backend stuff. I mean no complex patterns of listeners (for what? for Martians ?) - just bread-and-butter JS coding for tasks like sign-up, login, sign-out, add to mailing list, order product, make a payment, etc. My 2 cents worth.

    • @luiscerda4031
      @luiscerda4031 ปีที่แล้ว

      @@AllThingsJavaScript do you have any courses at Udemy?

  • @JustLookA7
    @JustLookA7 6 ปีที่แล้ว +1

    I love this, thanks!

  • @wrabac89
    @wrabac89 3 ปีที่แล้ว

    Awesome channel, happy i found it! (was searching for "javacript theory")

  • @everforexcanada5663
    @everforexcanada5663 ปีที่แล้ว

    great info!

  • @naofalhakim7820
    @naofalhakim7820 4 ปีที่แล้ว

    helpfull man, thanks. i like your detail explanation

  • @mattwetzel2294
    @mattwetzel2294 5 ปีที่แล้ว

    at @10:23 you mention a different technique for dealing with global variables in the namespace; what is that other technique?

    • @AllThingsJavaScript
      @AllThingsJavaScript  5 ปีที่แล้ว +1

      Basically you check to see if that variable exists and if it does you include what is defined in what you are creating.

  • @smde1
    @smde1 7 ปีที่แล้ว

    Superb video . Thanks.

  • @naukaprogramowania704
    @naukaprogramowania704 4 ปีที่แล้ว

    I wowed when I tested "the init" function :D

  • @luiscerda4031
    @luiscerda4031 ปีที่แล้ว

    I can't solve null from document.getElementById.

    • @AllThingsJavaScript
      @AllThingsJavaScript  ปีที่แล้ว

      If you are getting null from document.getElementById, either the element doesn't exist with that id or the DOM is loaded after the JavaScript runs.

    • @luiscerda4031
      @luiscerda4031 ปีที่แล้ว

      @@AllThingsJavaScript Thank you sir! Yes the document is loading last.
      May i ask is there a way to solve this?

  • @ainbrisk545
    @ainbrisk545 4 ปีที่แล้ว

    When I try to use this, it works perfectly for the most part, but I experience a big problem.
    All functions defined that use case/switch statements no longer work. For example, if I have an onclick event in my HTML that calls "myFunction()", and it has a switch statement, what happens within the case statements never happen. For example, if there is some global variable x, and it reads x and decides what to do depending on its value with a switch statement, even if x == 2 and it asks case: 2. I think it has something to do with the fact it can no longer read the variable used within the switch statement because global variables are all now local and encased by the function you said to add, but this shouldn't be a problem since they are in the same function.
    Any ideas?

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว

      Could you provide your code?

    • @ainbrisk545
      @ainbrisk545 4 ปีที่แล้ว

      ​@@AllThingsJavaScript Here is the github link to it:
      github.com/BrianZhang1/tournamentTimer
      Warning: It's long. Examples of functions that don't work are: toggleSound(), nextStep(), and reset(mode)

    • @ainbrisk545
      @ainbrisk545 4 ปีที่แล้ว

      @@AllThingsJavaScript Access the file named TournamentTimer Broken, that is the one that doesn't work. The other one works if you'd like to see how it normally operates.

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว

      @@ainbrisk545 Where did you provide the file?

    • @ainbrisk545
      @ainbrisk545 4 ปีที่แล้ว

      @@AllThingsJavaScript It is the github link, you can download the folder there with my files. I'll paste it again:
      github.com/BrianZhang1/tournamentTimer

  • @gouthambolt
    @gouthambolt 4 ปีที่แล้ว

    or you can just use type='module'. correct me if I ma wrong.

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว +1

      Yes, modules prevent global variable. Techniques discussed in this tutorial are the same techniques that the traditional module pattern uses and many libraries. ES6 modules (type='module') prevent global variables as well.

  • @AlAminKhan-on4ts
    @AlAminKhan-on4ts ปีที่แล้ว

    So this is how react was created 😁

  • @amguruprasath8037
    @amguruprasath8037 5 ปีที่แล้ว

    this video is repeated twice in your channel, sir.