Javascript prototype function undefined when constructor call in parentheses










1















Why does






function Example() 
this.go();

Example.prototype.go = function()
console.log("going");

(new Example());





bark at me that this.go is not a function whereas






function Example() 
this.go();

Example.prototype.go = function()
console.log("going");

new Example();





is logging correctly as expected?



Execution environment is node v10.4.1 on macOS High Sierra 10.13.4










share|improve this question



















  • 1





    As has been pointed out, it's a missing ;, May I advice you use an editor with built in linting, as things like this get picked up.

    – Keith
    Nov 16 '18 at 10:00











  • Following the advice now :-) Thanx.

    – On-The-Internet-Nobody-Knows
    Nov 18 '18 at 11:18















1















Why does






function Example() 
this.go();

Example.prototype.go = function()
console.log("going");

(new Example());





bark at me that this.go is not a function whereas






function Example() 
this.go();

Example.prototype.go = function()
console.log("going");

new Example();





is logging correctly as expected?



Execution environment is node v10.4.1 on macOS High Sierra 10.13.4










share|improve this question



















  • 1





    As has been pointed out, it's a missing ;, May I advice you use an editor with built in linting, as things like this get picked up.

    – Keith
    Nov 16 '18 at 10:00











  • Following the advice now :-) Thanx.

    – On-The-Internet-Nobody-Knows
    Nov 18 '18 at 11:18













1












1








1








Why does






function Example() 
this.go();

Example.prototype.go = function()
console.log("going");

(new Example());





bark at me that this.go is not a function whereas






function Example() 
this.go();

Example.prototype.go = function()
console.log("going");

new Example();





is logging correctly as expected?



Execution environment is node v10.4.1 on macOS High Sierra 10.13.4










share|improve this question
















Why does






function Example() 
this.go();

Example.prototype.go = function()
console.log("going");

(new Example());





bark at me that this.go is not a function whereas






function Example() 
this.go();

Example.prototype.go = function()
console.log("going");

new Example();





is logging correctly as expected?



Execution environment is node v10.4.1 on macOS High Sierra 10.13.4






function Example() 
this.go();

Example.prototype.go = function()
console.log("going");

(new Example());





function Example() 
this.go();

Example.prototype.go = function()
console.log("going");

(new Example());





function Example() 
this.go();

Example.prototype.go = function()
console.log("going");

new Example();





function Example() 
this.go();

Example.prototype.go = function()
console.log("going");

new Example();






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 9:52









Quentin

657k728941056




657k728941056










asked Nov 16 '18 at 9:50









On-The-Internet-Nobody-KnowsOn-The-Internet-Nobody-Knows

153




153







  • 1





    As has been pointed out, it's a missing ;, May I advice you use an editor with built in linting, as things like this get picked up.

    – Keith
    Nov 16 '18 at 10:00











  • Following the advice now :-) Thanx.

    – On-The-Internet-Nobody-Knows
    Nov 18 '18 at 11:18












  • 1





    As has been pointed out, it's a missing ;, May I advice you use an editor with built in linting, as things like this get picked up.

    – Keith
    Nov 16 '18 at 10:00











  • Following the advice now :-) Thanx.

    – On-The-Internet-Nobody-Knows
    Nov 18 '18 at 11:18







1




1





As has been pointed out, it's a missing ;, May I advice you use an editor with built in linting, as things like this get picked up.

– Keith
Nov 16 '18 at 10:00





As has been pointed out, it's a missing ;, May I advice you use an editor with built in linting, as things like this get picked up.

– Keith
Nov 16 '18 at 10:00













Following the advice now :-) Thanx.

– On-The-Internet-Nobody-Knows
Nov 18 '18 at 11:18





Following the advice now :-) Thanx.

– On-The-Internet-Nobody-Knows
Nov 18 '18 at 11:18












3 Answers
3






active

oldest

votes


















2














You are depending on ASI and it isn't working the way you expect.



In the first example, the () are turning the function expression you are trying to assign to Example.prototype.go into an IIFE.



Consequently, the order of events is:




  1. Example is defined


  2. new Example() is evaluated

  3. The result is passed as an argument in a call to the anonymous function

  4. The return value of that function (undefined) is assigned to Example.prototype.go

… except it errors at step 2 because go isn't defined at that point.



End each statement with an explicit semi-colon to avoid this.






share|improve this answer






























    2














    It's because you're relying on Automatic Semicolon Insertion (ASI) (perhaps unintentionally), but also starting a line with a (. If you correctly terminate the assignment with a ;, the problem goes away:






    function Example() 
    this.go();

    Example.prototype.go = function()
    console.log("going");
    ; // <==== ; here
    (new Example());





    The problem is that without the ;, the () expression following the assignment combines with the function() expression in front of it in a syntactically- (but not logically-) valid way: It calls the function, passing in the result of new Example. So ASI doesn't kick in.



    If you aren't intentionally relying on ASI, just note that assignment expressions need to be terminated with ;s.



    If you are intentionally relying on ASI, you must start lines that have a leading ( or [ with a proactive ; for this very reason. So in your case:



     function Example() 
    this.go();

    Example.prototype.go = function()
    console.log("going");

    ;(new Example());
    // ^----





    share|improve this answer






























      0














      The problem is that you forgot a semicolon after the function expression assigned to .go:



      Example.prototype.go = function() 
      console.log("going");
      // <--- no semicolon!
      (new Example());


      The following line beings with opening parentheses - thus, the interpreter thinks you're immediately calling the aforementioned function:



      Example.prototype.go = function() 
      console.log("going");
      (new Example());


      This is happening before Example.prototype.go has been assigned to - the interpreter is trying to evaluate the right-hand side of the expression to a value before assigning the result to Example.prototype.go. But you're calling new Example() before Example.prototype.go is populated; thus, this.go is undefined.



      Just put a semicolon after the } instead:



      Example.prototype.go = function() 
      console.log("going");
      ;
      (new Example());





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335257%2fjavascript-prototype-function-undefined-when-constructor-call-in-parentheses%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        You are depending on ASI and it isn't working the way you expect.



        In the first example, the () are turning the function expression you are trying to assign to Example.prototype.go into an IIFE.



        Consequently, the order of events is:




        1. Example is defined


        2. new Example() is evaluated

        3. The result is passed as an argument in a call to the anonymous function

        4. The return value of that function (undefined) is assigned to Example.prototype.go

        … except it errors at step 2 because go isn't defined at that point.



        End each statement with an explicit semi-colon to avoid this.






        share|improve this answer



























          2














          You are depending on ASI and it isn't working the way you expect.



          In the first example, the () are turning the function expression you are trying to assign to Example.prototype.go into an IIFE.



          Consequently, the order of events is:




          1. Example is defined


          2. new Example() is evaluated

          3. The result is passed as an argument in a call to the anonymous function

          4. The return value of that function (undefined) is assigned to Example.prototype.go

          … except it errors at step 2 because go isn't defined at that point.



          End each statement with an explicit semi-colon to avoid this.






          share|improve this answer

























            2












            2








            2







            You are depending on ASI and it isn't working the way you expect.



            In the first example, the () are turning the function expression you are trying to assign to Example.prototype.go into an IIFE.



            Consequently, the order of events is:




            1. Example is defined


            2. new Example() is evaluated

            3. The result is passed as an argument in a call to the anonymous function

            4. The return value of that function (undefined) is assigned to Example.prototype.go

            … except it errors at step 2 because go isn't defined at that point.



            End each statement with an explicit semi-colon to avoid this.






            share|improve this answer













            You are depending on ASI and it isn't working the way you expect.



            In the first example, the () are turning the function expression you are trying to assign to Example.prototype.go into an IIFE.



            Consequently, the order of events is:




            1. Example is defined


            2. new Example() is evaluated

            3. The result is passed as an argument in a call to the anonymous function

            4. The return value of that function (undefined) is assigned to Example.prototype.go

            … except it errors at step 2 because go isn't defined at that point.



            End each statement with an explicit semi-colon to avoid this.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 16 '18 at 9:54









            QuentinQuentin

            657k728941056




            657k728941056























                2














                It's because you're relying on Automatic Semicolon Insertion (ASI) (perhaps unintentionally), but also starting a line with a (. If you correctly terminate the assignment with a ;, the problem goes away:






                function Example() 
                this.go();

                Example.prototype.go = function()
                console.log("going");
                ; // <==== ; here
                (new Example());





                The problem is that without the ;, the () expression following the assignment combines with the function() expression in front of it in a syntactically- (but not logically-) valid way: It calls the function, passing in the result of new Example. So ASI doesn't kick in.



                If you aren't intentionally relying on ASI, just note that assignment expressions need to be terminated with ;s.



                If you are intentionally relying on ASI, you must start lines that have a leading ( or [ with a proactive ; for this very reason. So in your case:



                 function Example() 
                this.go();

                Example.prototype.go = function()
                console.log("going");

                ;(new Example());
                // ^----





                share|improve this answer



























                  2














                  It's because you're relying on Automatic Semicolon Insertion (ASI) (perhaps unintentionally), but also starting a line with a (. If you correctly terminate the assignment with a ;, the problem goes away:






                  function Example() 
                  this.go();

                  Example.prototype.go = function()
                  console.log("going");
                  ; // <==== ; here
                  (new Example());





                  The problem is that without the ;, the () expression following the assignment combines with the function() expression in front of it in a syntactically- (but not logically-) valid way: It calls the function, passing in the result of new Example. So ASI doesn't kick in.



                  If you aren't intentionally relying on ASI, just note that assignment expressions need to be terminated with ;s.



                  If you are intentionally relying on ASI, you must start lines that have a leading ( or [ with a proactive ; for this very reason. So in your case:



                   function Example() 
                  this.go();

                  Example.prototype.go = function()
                  console.log("going");

                  ;(new Example());
                  // ^----





                  share|improve this answer

























                    2












                    2








                    2







                    It's because you're relying on Automatic Semicolon Insertion (ASI) (perhaps unintentionally), but also starting a line with a (. If you correctly terminate the assignment with a ;, the problem goes away:






                    function Example() 
                    this.go();

                    Example.prototype.go = function()
                    console.log("going");
                    ; // <==== ; here
                    (new Example());





                    The problem is that without the ;, the () expression following the assignment combines with the function() expression in front of it in a syntactically- (but not logically-) valid way: It calls the function, passing in the result of new Example. So ASI doesn't kick in.



                    If you aren't intentionally relying on ASI, just note that assignment expressions need to be terminated with ;s.



                    If you are intentionally relying on ASI, you must start lines that have a leading ( or [ with a proactive ; for this very reason. So in your case:



                     function Example() 
                    this.go();

                    Example.prototype.go = function()
                    console.log("going");

                    ;(new Example());
                    // ^----





                    share|improve this answer













                    It's because you're relying on Automatic Semicolon Insertion (ASI) (perhaps unintentionally), but also starting a line with a (. If you correctly terminate the assignment with a ;, the problem goes away:






                    function Example() 
                    this.go();

                    Example.prototype.go = function()
                    console.log("going");
                    ; // <==== ; here
                    (new Example());





                    The problem is that without the ;, the () expression following the assignment combines with the function() expression in front of it in a syntactically- (but not logically-) valid way: It calls the function, passing in the result of new Example. So ASI doesn't kick in.



                    If you aren't intentionally relying on ASI, just note that assignment expressions need to be terminated with ;s.



                    If you are intentionally relying on ASI, you must start lines that have a leading ( or [ with a proactive ; for this very reason. So in your case:



                     function Example() 
                    this.go();

                    Example.prototype.go = function()
                    console.log("going");

                    ;(new Example());
                    // ^----





                    function Example() 
                    this.go();

                    Example.prototype.go = function()
                    console.log("going");
                    ; // <==== ; here
                    (new Example());





                    function Example() 
                    this.go();

                    Example.prototype.go = function()
                    console.log("going");
                    ; // <==== ; here
                    (new Example());






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 '18 at 9:54









                    T.J. CrowderT.J. Crowder

                    698k12312401336




                    698k12312401336





















                        0














                        The problem is that you forgot a semicolon after the function expression assigned to .go:



                        Example.prototype.go = function() 
                        console.log("going");
                        // <--- no semicolon!
                        (new Example());


                        The following line beings with opening parentheses - thus, the interpreter thinks you're immediately calling the aforementioned function:



                        Example.prototype.go = function() 
                        console.log("going");
                        (new Example());


                        This is happening before Example.prototype.go has been assigned to - the interpreter is trying to evaluate the right-hand side of the expression to a value before assigning the result to Example.prototype.go. But you're calling new Example() before Example.prototype.go is populated; thus, this.go is undefined.



                        Just put a semicolon after the } instead:



                        Example.prototype.go = function() 
                        console.log("going");
                        ;
                        (new Example());





                        share|improve this answer



























                          0














                          The problem is that you forgot a semicolon after the function expression assigned to .go:



                          Example.prototype.go = function() 
                          console.log("going");
                          // <--- no semicolon!
                          (new Example());


                          The following line beings with opening parentheses - thus, the interpreter thinks you're immediately calling the aforementioned function:



                          Example.prototype.go = function() 
                          console.log("going");
                          (new Example());


                          This is happening before Example.prototype.go has been assigned to - the interpreter is trying to evaluate the right-hand side of the expression to a value before assigning the result to Example.prototype.go. But you're calling new Example() before Example.prototype.go is populated; thus, this.go is undefined.



                          Just put a semicolon after the } instead:



                          Example.prototype.go = function() 
                          console.log("going");
                          ;
                          (new Example());





                          share|improve this answer

























                            0












                            0








                            0







                            The problem is that you forgot a semicolon after the function expression assigned to .go:



                            Example.prototype.go = function() 
                            console.log("going");
                            // <--- no semicolon!
                            (new Example());


                            The following line beings with opening parentheses - thus, the interpreter thinks you're immediately calling the aforementioned function:



                            Example.prototype.go = function() 
                            console.log("going");
                            (new Example());


                            This is happening before Example.prototype.go has been assigned to - the interpreter is trying to evaluate the right-hand side of the expression to a value before assigning the result to Example.prototype.go. But you're calling new Example() before Example.prototype.go is populated; thus, this.go is undefined.



                            Just put a semicolon after the } instead:



                            Example.prototype.go = function() 
                            console.log("going");
                            ;
                            (new Example());





                            share|improve this answer













                            The problem is that you forgot a semicolon after the function expression assigned to .go:



                            Example.prototype.go = function() 
                            console.log("going");
                            // <--- no semicolon!
                            (new Example());


                            The following line beings with opening parentheses - thus, the interpreter thinks you're immediately calling the aforementioned function:



                            Example.prototype.go = function() 
                            console.log("going");
                            (new Example());


                            This is happening before Example.prototype.go has been assigned to - the interpreter is trying to evaluate the right-hand side of the expression to a value before assigning the result to Example.prototype.go. But you're calling new Example() before Example.prototype.go is populated; thus, this.go is undefined.



                            Just put a semicolon after the } instead:



                            Example.prototype.go = function() 
                            console.log("going");
                            ;
                            (new Example());






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 16 '18 at 9:55









                            CertainPerformanceCertainPerformance

                            97.3k165887




                            97.3k165887



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335257%2fjavascript-prototype-function-undefined-when-constructor-call-in-parentheses%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Top Tejano songwriter Luis Silva dead of heart attack at 64

                                天津地下鉄3号線

                                How to fill missing numeric if any value in a subset is missing, all other columns with the same subset are missing