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. Other Discussions
  3. The Weird and The Wonderful
  4. Wtf JS?

Wtf JS?

Scheduled Pinned Locked Moved The Weird and The Wonderful
javascripthelpjavasalestools
7 Posts 4 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
    jan larsen
    wrote on last edited by
    #1

    So, I have to help rewriting our customer website, which involves getting my hands dirty on JavaScript. No script bashing here, the right tool for the right job and all. Deciding to involve the Revealing Module Pattern for my multi viewmodel per page scripting, I tried writing something like this:

    $(document).ready
    (
    function()
    {
    tmg.model1 = (function()
    {
    // Private
    var name = ko.observable("Model 1");

            // Public
            return
            {
                name: name
            };
    
        })();
    
        $("\[tmg\_ns='vm1'\]").each(
            function()
            {
                var domNode = $(this)\[0\];
                ko.applyBindings(tmg.model1, domNode);
            }
        );
    }
    

    );

    It didn't work... Debugging revealed that the name property was undefined. After several hours and a lot of :java:, I found the 'error'; When returning a JavaScript object, the curly brace must be on the same line as the return statement!!! Yes, 3 exclamation marks, see what you made me do JS?, and oh, I want my hours back.

    "God doesn't play dice" - Albert Einstein "God not only plays dice, He sometimes throws the dices where they cannot be seen" - Niels Bohr

    V J I 3 Replies Last reply
    0
    • J jan larsen

      So, I have to help rewriting our customer website, which involves getting my hands dirty on JavaScript. No script bashing here, the right tool for the right job and all. Deciding to involve the Revealing Module Pattern for my multi viewmodel per page scripting, I tried writing something like this:

      $(document).ready
      (
      function()
      {
      tmg.model1 = (function()
      {
      // Private
      var name = ko.observable("Model 1");

              // Public
              return
              {
                  name: name
              };
      
          })();
      
          $("\[tmg\_ns='vm1'\]").each(
              function()
              {
                  var domNode = $(this)\[0\];
                  ko.applyBindings(tmg.model1, domNode);
              }
          );
      }
      

      );

      It didn't work... Debugging revealed that the name property was undefined. After several hours and a lot of :java:, I found the 'error'; When returning a JavaScript object, the curly brace must be on the same line as the return statement!!! Yes, 3 exclamation marks, see what you made me do JS?, and oh, I want my hours back.

      "God doesn't play dice" - Albert Einstein "God not only plays dice, He sometimes throws the dices where they cannot be seen" - Niels Bohr

      V Offline
      V Offline
      Vark111
      wrote on last edited by
      #2

      The real wtf is Javascript's optional semi colons, which is what's happening there. Using something like JSLint would have told you this:

      {
      name: name
      };

      was unreachable code. But yeah, JS definitely has some... interesting pieces. :)

      J 1 Reply Last reply
      0
      • J jan larsen

        So, I have to help rewriting our customer website, which involves getting my hands dirty on JavaScript. No script bashing here, the right tool for the right job and all. Deciding to involve the Revealing Module Pattern for my multi viewmodel per page scripting, I tried writing something like this:

        $(document).ready
        (
        function()
        {
        tmg.model1 = (function()
        {
        // Private
        var name = ko.observable("Model 1");

                // Public
                return
                {
                    name: name
                };
        
            })();
        
            $("\[tmg\_ns='vm1'\]").each(
                function()
                {
                    var domNode = $(this)\[0\];
                    ko.applyBindings(tmg.model1, domNode);
                }
            );
        }
        

        );

        It didn't work... Debugging revealed that the name property was undefined. After several hours and a lot of :java:, I found the 'error'; When returning a JavaScript object, the curly brace must be on the same line as the return statement!!! Yes, 3 exclamation marks, see what you made me do JS?, and oh, I want my hours back.

        "God doesn't play dice" - Albert Einstein "God not only plays dice, He sometimes throws the dices where they cannot be seen" - Niels Bohr

        J Offline
        J Offline
        jsc42
        wrote on last edited by
        #3

        Ah! The old semicolon-less syntax designed to help ex VB programmers. The newline after return marks the end of the return statement and the following braced bit is ignored. It is not true that the { has to be on the same line. A better format is

        var result = { name: name };
        return result;

        or, much easier, discard the function completely and just write

        tmg.model1 = { name: ko.observable("Model 1") };

        J 1 Reply Last reply
        0
        • V Vark111

          The real wtf is Javascript's optional semi colons, which is what's happening there. Using something like JSLint would have told you this:

          {
          name: name
          };

          was unreachable code. But yeah, JS definitely has some... interesting pieces. :)

          J Offline
          J Offline
          jan larsen
          wrote on last edited by
          #4

          Heh, thanks. Allthough it's an interesting project, I just don't think I will learn to love JS.

          "God doesn't play dice" - Albert Einstein "God not only plays dice, He sometimes throws the dices where they cannot be seen" - Niels Bohr

          1 Reply Last reply
          0
          • J jsc42

            Ah! The old semicolon-less syntax designed to help ex VB programmers. The newline after return marks the end of the return statement and the following braced bit is ignored. It is not true that the { has to be on the same line. A better format is

            var result = { name: name };
            return result;

            or, much easier, discard the function completely and just write

            tmg.model1 = { name: ko.observable("Model 1") };

            J Offline
            J Offline
            jan larsen
            wrote on last edited by
            #5

            Thanks for clearing that up. Didn't make JS look any better to me though :) Weird architecture that, just because there is a structure doesn't mean that it's actually helpfull. I'm sure there is a special part of Hell reserved for script designers putting subtle twists to a product. I will certainly go for your first suggestion, the second wouldn't conform to the Revealing Module Pattern (or should that have been in all caps?).

            "God doesn't play dice" - Albert Einstein "God not only plays dice, He sometimes throws the dices where they cannot be seen" - Niels Bohr

            V 1 Reply Last reply
            0
            • J jan larsen

              Thanks for clearing that up. Didn't make JS look any better to me though :) Weird architecture that, just because there is a structure doesn't mean that it's actually helpfull. I'm sure there is a special part of Hell reserved for script designers putting subtle twists to a product. I will certainly go for your first suggestion, the second wouldn't conform to the Revealing Module Pattern (or should that have been in all caps?).

              "God doesn't play dice" - Albert Einstein "God not only plays dice, He sometimes throws the dices where they cannot be seen" - Niels Bohr

              V Offline
              V Offline
              Vark111
              wrote on last edited by
              #6

              jan larsen wrote:

              I'm sure there is a special part of Hell reserved for script designers putting subtle twists to a product.

              Well, in Mr. Eich's defense, he did only have 10 days to design the language. :)

              1 Reply Last reply
              0
              • J jan larsen

                So, I have to help rewriting our customer website, which involves getting my hands dirty on JavaScript. No script bashing here, the right tool for the right job and all. Deciding to involve the Revealing Module Pattern for my multi viewmodel per page scripting, I tried writing something like this:

                $(document).ready
                (
                function()
                {
                tmg.model1 = (function()
                {
                // Private
                var name = ko.observable("Model 1");

                        // Public
                        return
                        {
                            name: name
                        };
                
                    })();
                
                    $("\[tmg\_ns='vm1'\]").each(
                        function()
                        {
                            var domNode = $(this)\[0\];
                            ko.applyBindings(tmg.model1, domNode);
                        }
                    );
                }
                

                );

                It didn't work... Debugging revealed that the name property was undefined. After several hours and a lot of :java:, I found the 'error'; When returning a JavaScript object, the curly brace must be on the same line as the return statement!!! Yes, 3 exclamation marks, see what you made me do JS?, and oh, I want my hours back.

                "God doesn't play dice" - Albert Einstein "God not only plays dice, He sometimes throws the dices where they cannot be seen" - Niels Bohr

                I Offline
                I Offline
                imagiro
                wrote on last edited by
                #7

                That's probably part of the reason why Allman style is not very popular in JS. And I agree: The optional semicolon is just terrible.

                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