How to Create Singleton Classes in JavaScript - Design Patterns Tutorial

Поділитися
Вставка
  • Опубліковано 14 жов 2024
  • In this video tutorial I'll be showing you a technique to create a Singleton Class in JavaScript with ES6 classes.
    Singleton classes are used when you only want a single instance of a class to be created - this is useful for things like "settings" or "environmental variable" accessors.
    Support me on Patreon:
    / dcode - with enough funding I plan to develop a website of some sort with a new developer experience!
    Follow me on Twitter @dcodeyt!
    If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
    #dcode #javascript #webdev

КОМЕНТАРІ • 40

  • @gmjitendra
    @gmjitendra Рік тому

    Excellent example. Thanks

  • @smoothbeak
    @smoothbeak 5 років тому +2

    That was pretty interesting, not something I had ever considered doing before

    • @dcode-software
      @dcode-software  5 років тому +2

      Yeah mate, most cases you probably don't need it, but it has potential to inspire something else.

    • @smoothbeak
      @smoothbeak 5 років тому +1

      @@dcode-software For sure, I thought it was quite clever

  • @montebont
    @montebont 6 днів тому

    Nice one but looks a bit like a solution looking for a problem.
    From my (PHP) background this looks like a factory pattern which produces a new class instance. Static methods in the factory will do just that...🙂 And IMHO that's the real use case

  • @DusanResin
    @DusanResin 5 років тому +1

    Very nice solution! Thanks for sharing.

  • @adampielach4942
    @adampielach4942 5 років тому +1

    Great video! Keep 'em comming, Dom!

  • @mattcroat
    @mattcroat 5 років тому +1

    Thanks! I'm going to look more into design patterns 👍

  • @nueno3816
    @nueno3816 3 роки тому

    you can even do "class Settings { static shared = new Settings() /* .. */ }" so it's obvious for any reader that Settings is a singleton, even without looking into the implementation of the Settings class.

  • @LearnwithAvinashDalvi
    @LearnwithAvinashDalvi 3 роки тому

    Nicely explained.

  • @shubhammandal4307
    @shubhammandal4307 3 роки тому

    Thanks For Teaching

  • @SebaRealino
    @SebaRealino 3 роки тому

    Thanks! Helped a lot!

  • @cod9955
    @cod9955 5 років тому +1

    Thanku very much subbed!!

  • @rasharun5683
    @rasharun5683 3 роки тому

    thank you so much

  • @chamnil8666
    @chamnil8666 4 роки тому +1

    Sir, please explain why did you say chance of one in four thousand nearly at the end of the tutorial, for getting the same objects.

    • @tigrafale4610
      @tigrafale4610 2 роки тому +1

      cus he added a random number for the version number from 1 to 4000, so if it weren't a singleton, then there would be a 1 in 4000 chance that the two objects would get the same version number

  • @munafhajir4343
    @munafhajir4343 4 роки тому

    Nice Video, need more videos like this want to understand how to select best structure and design pattern for your application

  • @Colstonewall
    @Colstonewall 5 років тому +1

    Thank you.

  • @bradical8198
    @bradical8198 4 роки тому

    So what if two users are using the same website, would they be sharing the same values in the constructor?

  • @onthefly--4834
    @onthefly--4834 4 роки тому

    Hi, thanks for this tutorial.
    Here's a question. In my case I load another html file, and then going from href to another html file.
    On that file, the instance is no longer defined for some reason, can you think of a reason why that is?
    Thanks, Shefy

    • @wilsongaturu134
      @wilsongaturu134 4 роки тому

      If its not a single page app, navigating away from the current page will make new http requests and rerun all JavaScript. It creates a new instance of the browser window.

  • @ginnerzapata5909
    @ginnerzapata5909 4 роки тому

    thanks a lot for your videos on OOP with JS classes

  • @matheusicaro8815
    @matheusicaro8815 4 роки тому

    What this a theme? It's pretty!
    Thanks for video.

  • @pandeyperfect8292
    @pandeyperfect8292 2 роки тому

    nice

  • @SoldierXfreedom
    @SoldierXfreedom 3 роки тому

    Whats the point of using a class for this? Cant I just use a Settings object that way I dont have to create an instance?

  • @eduardoezequiel8746
    @eduardoezequiel8746 2 роки тому

    font? nice video!

  • @SuraajSuresh1409
    @SuraajSuresh1409 5 років тому

    Great👌

  • @eugenenovikov671
    @eugenenovikov671 3 роки тому

    спасибо большое

  • @jofftiquez
    @jofftiquez 4 роки тому +1

    Neat!

  • @rainsaarmae1863
    @rainsaarmae1863 4 роки тому

    Using VS code to develop node.js.
    I'm getting error "Property 'instance' does not exist on type 'typeof Settings'."

    • @rainsaarmae1863
      @rainsaarmae1863 4 роки тому

      Nevermind it was my tslint being overzealous and validating JS files

  • @taylorgraham2906
    @taylorgraham2906 Рік тому

    Keyboard is too loud. Good video though