MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Why are people doing this: var self = this?

    Scheduled Pinned Locked Moved Development
    5 Posts 3 Posters 2.8k Views 3 Watching
    Loading More Posts
    • 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.
    • E Offline
      E3V3A
      last edited by

      I keep seeing modules doing the following:

      socketNotificationReceived: function(notification, payload) {
          var self = this;
          if (notification === "CURRENT_STATUS") {
      ...
      
      • Can someone explain why they do this?
      • What make this change so that you need to save it?

      "Everything I do (here) is for free – altruism is the way!"
      MMM-FlightsAbove, MMM-Tabulator, MMM-Assistant (co-maintainer)

      N 1 Reply Last reply Reply Quote 0
      • D Offline
        doubleT Module Developer
        last edited by

        @E3V3A said in Why are people doing this: var self = this?:

        var self = this

        this can change. When it’s called first, it’s one thing and when you think you need the same thing again, this might be something else already. So you temporarily store the first this in the var self. That way it’s the same thing when you need it later again.

        E 1 Reply Last reply Reply Quote 0
        • E Offline
          E3V3A @doubleT
          last edited by

          @doubleT Hi, yeah I figured that much. But in the cases where I have seen it, it is not obvious what would change it outside that function. In the example, from above, was taken from the MMM-Remote-Control, where it’s done all over the place. So if you are that paranoid that it can change that easily, it could as well change in between function calls, before even setting var self=this? Leading to the question of:

          • What is the exact scope of this?
          • Does the core MM change it or use it as well?

          "Everything I do (here) is for free – altruism is the way!"
          MMM-FlightsAbove, MMM-Tabulator, MMM-Assistant (co-maintainer)

          1 Reply Last reply Reply Quote 0
          • N Offline
            ninjabreadman @E3V3A
            last edited by

            @E3V3A This seems like a pretty comprehensive primer, including the particularities of this in JavaScript.

            Problem with config or JavaScript? Copy/paste it into JSHint.
            Check out the detailed walkthroughs on install, config, modules, etc.

            E 1 Reply Last reply Reply Quote 1
            • E Offline
              E3V3A @ninjabreadman
              last edited by E3V3A

              @ninjabreadman Great Find! Exactly what I needed. That it seem a great lesson on this.

              In the reference…

              To fix the problem with using this inside the anonymous function passed to the "forEach" 
              method, we use a common practice in JavaScript and set the this value to another 
              variable before we enter the "forEach" method.
              

              "Everything I do (here) is for free – altruism is the way!"
              MMM-FlightsAbove, MMM-Tabulator, MMM-Assistant (co-maintainer)

              1 Reply Last reply Reply Quote 0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • 1 / 1
              • First post
                Last post
              Enjoying MagicMirror? Please consider a donation!
              MagicMirror created by Michael Teeuw.
              Forum managed by Sam, technical setup by Karsten.
              This forum is using NodeBB as its core | Contributors
              Contact | Privacy Policy