Are prototypes needed in JavaScript, when we have closures?












3














I have been working as a web developer for 3 years. I have only very seldom created my own prototypes for objects (I guess I also created prototypes via the new ES 6 class syntax).



Whenever I can do something with a prototype:



function Client(host, port) {
this.host = host;
this.port = port;
}

Client.prototype.httpRequestHelper = function(body) {
...
};

var client = new Client('http://127.0.0.1', 8801);


using closures instead has worked just fine, and has been the preferred approach because more people understand that approach well (me included):



function createClient(host, port) {
function httpRequestHelper(body) {
...
}

return {
httpRequestHelper
}
}

var client = createClient('http://127.0.0.1', 8801);


What are the benefits of using prototypes (and ES6 classes) over closures? Why are they needed?










share|improve this question





























    3














    I have been working as a web developer for 3 years. I have only very seldom created my own prototypes for objects (I guess I also created prototypes via the new ES 6 class syntax).



    Whenever I can do something with a prototype:



    function Client(host, port) {
    this.host = host;
    this.port = port;
    }

    Client.prototype.httpRequestHelper = function(body) {
    ...
    };

    var client = new Client('http://127.0.0.1', 8801);


    using closures instead has worked just fine, and has been the preferred approach because more people understand that approach well (me included):



    function createClient(host, port) {
    function httpRequestHelper(body) {
    ...
    }

    return {
    httpRequestHelper
    }
    }

    var client = createClient('http://127.0.0.1', 8801);


    What are the benefits of using prototypes (and ES6 classes) over closures? Why are they needed?










    share|improve this question



























      3












      3








      3







      I have been working as a web developer for 3 years. I have only very seldom created my own prototypes for objects (I guess I also created prototypes via the new ES 6 class syntax).



      Whenever I can do something with a prototype:



      function Client(host, port) {
      this.host = host;
      this.port = port;
      }

      Client.prototype.httpRequestHelper = function(body) {
      ...
      };

      var client = new Client('http://127.0.0.1', 8801);


      using closures instead has worked just fine, and has been the preferred approach because more people understand that approach well (me included):



      function createClient(host, port) {
      function httpRequestHelper(body) {
      ...
      }

      return {
      httpRequestHelper
      }
      }

      var client = createClient('http://127.0.0.1', 8801);


      What are the benefits of using prototypes (and ES6 classes) over closures? Why are they needed?










      share|improve this question















      I have been working as a web developer for 3 years. I have only very seldom created my own prototypes for objects (I guess I also created prototypes via the new ES 6 class syntax).



      Whenever I can do something with a prototype:



      function Client(host, port) {
      this.host = host;
      this.port = port;
      }

      Client.prototype.httpRequestHelper = function(body) {
      ...
      };

      var client = new Client('http://127.0.0.1', 8801);


      using closures instead has worked just fine, and has been the preferred approach because more people understand that approach well (me included):



      function createClient(host, port) {
      function httpRequestHelper(body) {
      ...
      }

      return {
      httpRequestHelper
      }
      }

      var client = createClient('http://127.0.0.1', 8801);


      What are the benefits of using prototypes (and ES6 classes) over closures? Why are they needed?







      javascript class closures prototype






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 15:20







      user1283776

















      asked Nov 21 '18 at 7:58









      user1283776user1283776

      3,1752155107




      3,1752155107
























          1 Answer
          1






          active

          oldest

          votes


















          3














          Well, with closures you lose the ability to duck type on properties or to apply Javascript's built-in introspection means (instanceof, isPrototypeOf). Furthermore, a closure's free variables differ from object properties in that they are not visible in the surrounding scope. Sure, you can define getters and setters to access them, but this is a bit awkward and you still can't utilize destructuring assignment and probably other core concepts of the language.



          So yes, closures and prototypes have some applications in common and even though they are not completely orthogonal to each other, they are still discrete concepts.






          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%2f53407520%2fare-prototypes-needed-in-javascript-when-we-have-closures%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            Well, with closures you lose the ability to duck type on properties or to apply Javascript's built-in introspection means (instanceof, isPrototypeOf). Furthermore, a closure's free variables differ from object properties in that they are not visible in the surrounding scope. Sure, you can define getters and setters to access them, but this is a bit awkward and you still can't utilize destructuring assignment and probably other core concepts of the language.



            So yes, closures and prototypes have some applications in common and even though they are not completely orthogonal to each other, they are still discrete concepts.






            share|improve this answer




























              3














              Well, with closures you lose the ability to duck type on properties or to apply Javascript's built-in introspection means (instanceof, isPrototypeOf). Furthermore, a closure's free variables differ from object properties in that they are not visible in the surrounding scope. Sure, you can define getters and setters to access them, but this is a bit awkward and you still can't utilize destructuring assignment and probably other core concepts of the language.



              So yes, closures and prototypes have some applications in common and even though they are not completely orthogonal to each other, they are still discrete concepts.






              share|improve this answer


























                3












                3








                3






                Well, with closures you lose the ability to duck type on properties or to apply Javascript's built-in introspection means (instanceof, isPrototypeOf). Furthermore, a closure's free variables differ from object properties in that they are not visible in the surrounding scope. Sure, you can define getters and setters to access them, but this is a bit awkward and you still can't utilize destructuring assignment and probably other core concepts of the language.



                So yes, closures and prototypes have some applications in common and even though they are not completely orthogonal to each other, they are still discrete concepts.






                share|improve this answer














                Well, with closures you lose the ability to duck type on properties or to apply Javascript's built-in introspection means (instanceof, isPrototypeOf). Furthermore, a closure's free variables differ from object properties in that they are not visible in the surrounding scope. Sure, you can define getters and setters to access them, but this is a bit awkward and you still can't utilize destructuring assignment and probably other core concepts of the language.



                So yes, closures and prototypes have some applications in common and even though they are not completely orthogonal to each other, they are still discrete concepts.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 23 '18 at 10:34

























                answered Nov 23 '18 at 9:30









                reifyreify

                28917




                28917






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53407520%2fare-prototypes-needed-in-javascript-when-we-have-closures%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

                    Ottavio Pratesi

                    Tricia Helfer

                    15 giugno