Function count calls











up vote
5
down vote

favorite












I'm a beginner with JavaScript so please be patient =)



I am trying to write a function that counts the number of times it is called. What I have so far is a function with a counter that is incremented explicitly:



var increment = function () {
var i = 0;
this.inc = function () {i += 1;};
this.get = function () {return i;};
};

var ob = new increment();
ob.inc();
ob.inc();
alert(ob.get());


But I'm wondering how to call only ob();, so the function could increment calls made to itself automatically. Is this possible and if so, how?










share|improve this question




























    up vote
    5
    down vote

    favorite












    I'm a beginner with JavaScript so please be patient =)



    I am trying to write a function that counts the number of times it is called. What I have so far is a function with a counter that is incremented explicitly:



    var increment = function () {
    var i = 0;
    this.inc = function () {i += 1;};
    this.get = function () {return i;};
    };

    var ob = new increment();
    ob.inc();
    ob.inc();
    alert(ob.get());


    But I'm wondering how to call only ob();, so the function could increment calls made to itself automatically. Is this possible and if so, how?










    share|improve this question


























      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      I'm a beginner with JavaScript so please be patient =)



      I am trying to write a function that counts the number of times it is called. What I have so far is a function with a counter that is incremented explicitly:



      var increment = function () {
      var i = 0;
      this.inc = function () {i += 1;};
      this.get = function () {return i;};
      };

      var ob = new increment();
      ob.inc();
      ob.inc();
      alert(ob.get());


      But I'm wondering how to call only ob();, so the function could increment calls made to itself automatically. Is this possible and if so, how?










      share|improve this question















      I'm a beginner with JavaScript so please be patient =)



      I am trying to write a function that counts the number of times it is called. What I have so far is a function with a counter that is incremented explicitly:



      var increment = function () {
      var i = 0;
      this.inc = function () {i += 1;};
      this.get = function () {return i;};
      };

      var ob = new increment();
      ob.inc();
      ob.inc();
      alert(ob.get());


      But I'm wondering how to call only ob();, so the function could increment calls made to itself automatically. Is this possible and if so, how?







      javascript function closures






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 31 '11 at 6:45









      Ray Toal

      64.7k10118174




      64.7k10118174










      asked Aug 30 '11 at 12:26









      Kamil

      2612




      2612
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          7
          down vote













          var increment = function() {
          var i = 0;
          return function() { return i += 1; };
          };

          var ob = increment();





          share|improve this answer

















          • 2




            +1 for throwing closures to a noob :)
            – naveen
            Aug 30 '11 at 12:36






          • 1




            +1. @naveen: The noob seems to be heading that way on his/her own. :)
            – Shef
            Aug 30 '11 at 12:38












          • @Shef: I said its good. Its a while before I realized there were closures. But then js was my secondary language. a tiny perk that has alertboxes :)
            – naveen
            Aug 30 '11 at 12:43






          • 1




            @naveen: I just agreed with you. :) I, too, was surprised to see the OP, a self-declared beginner, trying to learn closures.
            – Shef
            Aug 30 '11 at 12:50






          • 2




            @Vaibhav: The increment function itself doesn't increment anything. It returns a function that does. ob holds that function.
            – Marcelo Cantos
            Apr 14 '15 at 9:48




















          up vote
          0
          down vote













          ob = function (){  
          ++ob.i || (ob.i=1);
          return ob.i;
          }





          share|improve this answer






























            up vote
            0
            down vote













            Wrap a counter to any function:



            /**
            * Wrap a counter to a function
            * Count how many times a function is called
            * @param {Function} fn Function to count
            * @param {Number} count Counter, default to 1
            */
            function addCounterToFn(fn, count = 1) {
            return function () {
            fn.apply(null, arguments);
            return count++;
            }
            }


            See https://jsfiddle.net/n50eszwm/






            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',
              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%2f7243101%2ffunction-count-calls%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








              up vote
              7
              down vote













              var increment = function() {
              var i = 0;
              return function() { return i += 1; };
              };

              var ob = increment();





              share|improve this answer

















              • 2




                +1 for throwing closures to a noob :)
                – naveen
                Aug 30 '11 at 12:36






              • 1




                +1. @naveen: The noob seems to be heading that way on his/her own. :)
                – Shef
                Aug 30 '11 at 12:38












              • @Shef: I said its good. Its a while before I realized there were closures. But then js was my secondary language. a tiny perk that has alertboxes :)
                – naveen
                Aug 30 '11 at 12:43






              • 1




                @naveen: I just agreed with you. :) I, too, was surprised to see the OP, a self-declared beginner, trying to learn closures.
                – Shef
                Aug 30 '11 at 12:50






              • 2




                @Vaibhav: The increment function itself doesn't increment anything. It returns a function that does. ob holds that function.
                – Marcelo Cantos
                Apr 14 '15 at 9:48

















              up vote
              7
              down vote













              var increment = function() {
              var i = 0;
              return function() { return i += 1; };
              };

              var ob = increment();





              share|improve this answer

















              • 2




                +1 for throwing closures to a noob :)
                – naveen
                Aug 30 '11 at 12:36






              • 1




                +1. @naveen: The noob seems to be heading that way on his/her own. :)
                – Shef
                Aug 30 '11 at 12:38












              • @Shef: I said its good. Its a while before I realized there were closures. But then js was my secondary language. a tiny perk that has alertboxes :)
                – naveen
                Aug 30 '11 at 12:43






              • 1




                @naveen: I just agreed with you. :) I, too, was surprised to see the OP, a self-declared beginner, trying to learn closures.
                – Shef
                Aug 30 '11 at 12:50






              • 2




                @Vaibhav: The increment function itself doesn't increment anything. It returns a function that does. ob holds that function.
                – Marcelo Cantos
                Apr 14 '15 at 9:48















              up vote
              7
              down vote










              up vote
              7
              down vote









              var increment = function() {
              var i = 0;
              return function() { return i += 1; };
              };

              var ob = increment();





              share|improve this answer












              var increment = function() {
              var i = 0;
              return function() { return i += 1; };
              };

              var ob = increment();






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 30 '11 at 12:30









              Marcelo Cantos

              142k31279326




              142k31279326








              • 2




                +1 for throwing closures to a noob :)
                – naveen
                Aug 30 '11 at 12:36






              • 1




                +1. @naveen: The noob seems to be heading that way on his/her own. :)
                – Shef
                Aug 30 '11 at 12:38












              • @Shef: I said its good. Its a while before I realized there were closures. But then js was my secondary language. a tiny perk that has alertboxes :)
                – naveen
                Aug 30 '11 at 12:43






              • 1




                @naveen: I just agreed with you. :) I, too, was surprised to see the OP, a self-declared beginner, trying to learn closures.
                – Shef
                Aug 30 '11 at 12:50






              • 2




                @Vaibhav: The increment function itself doesn't increment anything. It returns a function that does. ob holds that function.
                – Marcelo Cantos
                Apr 14 '15 at 9:48
















              • 2




                +1 for throwing closures to a noob :)
                – naveen
                Aug 30 '11 at 12:36






              • 1




                +1. @naveen: The noob seems to be heading that way on his/her own. :)
                – Shef
                Aug 30 '11 at 12:38












              • @Shef: I said its good. Its a while before I realized there were closures. But then js was my secondary language. a tiny perk that has alertboxes :)
                – naveen
                Aug 30 '11 at 12:43






              • 1




                @naveen: I just agreed with you. :) I, too, was surprised to see the OP, a self-declared beginner, trying to learn closures.
                – Shef
                Aug 30 '11 at 12:50






              • 2




                @Vaibhav: The increment function itself doesn't increment anything. It returns a function that does. ob holds that function.
                – Marcelo Cantos
                Apr 14 '15 at 9:48










              2




              2




              +1 for throwing closures to a noob :)
              – naveen
              Aug 30 '11 at 12:36




              +1 for throwing closures to a noob :)
              – naveen
              Aug 30 '11 at 12:36




              1




              1




              +1. @naveen: The noob seems to be heading that way on his/her own. :)
              – Shef
              Aug 30 '11 at 12:38






              +1. @naveen: The noob seems to be heading that way on his/her own. :)
              – Shef
              Aug 30 '11 at 12:38














              @Shef: I said its good. Its a while before I realized there were closures. But then js was my secondary language. a tiny perk that has alertboxes :)
              – naveen
              Aug 30 '11 at 12:43




              @Shef: I said its good. Its a while before I realized there were closures. But then js was my secondary language. a tiny perk that has alertboxes :)
              – naveen
              Aug 30 '11 at 12:43




              1




              1




              @naveen: I just agreed with you. :) I, too, was surprised to see the OP, a self-declared beginner, trying to learn closures.
              – Shef
              Aug 30 '11 at 12:50




              @naveen: I just agreed with you. :) I, too, was surprised to see the OP, a self-declared beginner, trying to learn closures.
              – Shef
              Aug 30 '11 at 12:50




              2




              2




              @Vaibhav: The increment function itself doesn't increment anything. It returns a function that does. ob holds that function.
              – Marcelo Cantos
              Apr 14 '15 at 9:48






              @Vaibhav: The increment function itself doesn't increment anything. It returns a function that does. ob holds that function.
              – Marcelo Cantos
              Apr 14 '15 at 9:48














              up vote
              0
              down vote













              ob = function (){  
              ++ob.i || (ob.i=1);
              return ob.i;
              }





              share|improve this answer



























                up vote
                0
                down vote













                ob = function (){  
                ++ob.i || (ob.i=1);
                return ob.i;
                }





                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  ob = function (){  
                  ++ob.i || (ob.i=1);
                  return ob.i;
                  }





                  share|improve this answer














                  ob = function (){  
                  ++ob.i || (ob.i=1);
                  return ob.i;
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 14 '17 at 21:23

























                  answered Mar 14 '17 at 20:53









                  aljgom

                  1,146812




                  1,146812






















                      up vote
                      0
                      down vote













                      Wrap a counter to any function:



                      /**
                      * Wrap a counter to a function
                      * Count how many times a function is called
                      * @param {Function} fn Function to count
                      * @param {Number} count Counter, default to 1
                      */
                      function addCounterToFn(fn, count = 1) {
                      return function () {
                      fn.apply(null, arguments);
                      return count++;
                      }
                      }


                      See https://jsfiddle.net/n50eszwm/






                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Wrap a counter to any function:



                        /**
                        * Wrap a counter to a function
                        * Count how many times a function is called
                        * @param {Function} fn Function to count
                        * @param {Number} count Counter, default to 1
                        */
                        function addCounterToFn(fn, count = 1) {
                        return function () {
                        fn.apply(null, arguments);
                        return count++;
                        }
                        }


                        See https://jsfiddle.net/n50eszwm/






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Wrap a counter to any function:



                          /**
                          * Wrap a counter to a function
                          * Count how many times a function is called
                          * @param {Function} fn Function to count
                          * @param {Number} count Counter, default to 1
                          */
                          function addCounterToFn(fn, count = 1) {
                          return function () {
                          fn.apply(null, arguments);
                          return count++;
                          }
                          }


                          See https://jsfiddle.net/n50eszwm/






                          share|improve this answer












                          Wrap a counter to any function:



                          /**
                          * Wrap a counter to a function
                          * Count how many times a function is called
                          * @param {Function} fn Function to count
                          * @param {Number} count Counter, default to 1
                          */
                          function addCounterToFn(fn, count = 1) {
                          return function () {
                          fn.apply(null, arguments);
                          return count++;
                          }
                          }


                          See https://jsfiddle.net/n50eszwm/







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 10 at 23:46









                          pldg

                          412417




                          412417






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7243101%2ffunction-count-calls%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

                              Xamarin.iOS Cant Deploy on Iphone

                              Glorious Revolution

                              Dulmage-Mendelsohn matrix decomposition in Python