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.

    What is the difference between these function definitions?

    Scheduled Pinned Locked Moved Development
    3 Posts 2 Posters 1.6k Views 2 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 on seeing people adding their own functions in their MMM-Module.js.
      Some people write:

        updateDomIfNeeded: function() {
        ...
        }
      

      while others prefer to just write:

      myfunction() {
      ...
      }
      

      What is the difference to MM, if any?

      "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
      • N Offline
        ninjabreadman @E3V3A
        last edited by ninjabreadman

        @E3V3A The former is object notation; therefore, you need to be defining an object. For example:

        var Lemon = {
          skinColor: "yellow",
          juice: 10, // in mL
          squeeze: function() {
            return this.juice;
          }
        }
        

        That way, for each Lemon you can call Lemon.squeeze(). It is relative to itself; the Lemon returns its juice, that it only knows about because it’s a complete object.

        Meanwhile, if you have myFunction() it doesn’t belong to an object. It just exists. You can call it from anywhere as myFunction(). You can even assign it to another variable and call it from there (this is also why you can pass a callback function as an argument):

        var myFunction = new function(){
        ...
        }
        var otherFunction = myFunction;
        otherFunction();
        

        You can also have “lambda” functions that have no name.

        But, to answer your question, you can only use the “object” style if you’re in the middle of defining an object. An “object” style function (most often called a “method” of the object) can also reference the object itself, whereas as a function without an object needs it to be passed as a parameter, such as squeeze(someLemon).

        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 2
        • E Offline
          E3V3A @ninjabreadman
          last edited by E3V3A

          @ninjabreadman Fantastic! Again, the best explanation I’ve heard! I love that juice analogy!

          Thank you so much for having patience with all my dumb questions. Surely a lot of them are trivial to most developers. (If you’re a moderator, feel free to mark this as solved.)

          "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
          • 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