Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #24832

Re: Bind custom function to the all Function objects

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Bind custom function to the all Function objects
Date 2014-06-14 01:57 +0200
Organization PointedEars Software (PES)
Message-ID <1464959.oZ30Yp1K6V@PointedEars.de> (permalink)
References <98e91$539b0b89$6def49ce$21580@nntpswitch.blueworldhosting.com> <1649322.pjBWu2vbqe@PointedEars.de> <94a15$539b16e4$6def49ce$21821@nntpswitch.blueworldhosting.com> <2459085.3y454gxjXg@PointedEars.de> <1e6b6$539b6200$6def49ce$25031@nntpswitch.blueworldhosting.com>

Show all headers | View raw


Cezary Tomczyk wrote:

> 2014-06-13 19:01, Thomas 'PointedEars' Lahn wrote:
>> Cezary Tomczyk wrote:
>>> 2014-06-13 16:56, Thomas 'PointedEars' Lahn wrote:
>>>> Cezary Tomczyk wrote:
>>>>> I am looking for a solution where I can bind custom function to the
>>>>> all Function objects. Whenever any function/method will be called I'd
>>>>> like to call my custom function. I am going to use it only for
>>>>> testing/debugging. Doesn't have to be cross-browser.
>>>>                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>> What does this mean?
>>>
>>> This means that if one browser support required functionality then I'll
>>> use that specified browser in my case.
>>
>> Learn to think in terms of versions of implementations, not browsers.
> 
> Agreed. However before I start think in terms of versions of
> implementations I need to figure out those implementations

So you should ask “Which implementation X supports feature Y?” and then 
“Which browser Z supports implementation X?”, and not “Which browser Z 
supports feature Y?”

> Based on your previous answer there is nothing I can find and use in my
> case (below details).

Do not jump to conclusions.  Your case was not at all clear.
 
> Let me rephrase it. When I run the web application in the browser
> environment then I'd like to check how many times every
> functions/methods are called. The simplest way is to add console.count
> (https://developer.mozilla.org/en-US/docs/Web/API/console.count) to
> every function/method.

Thanks, I did not know of that method.
 
> However, I think that in reality it's not possible to do that because
> then changes needs to be done everywhere in the code. So, I thought that
> it would be better to "wrap" every function/method globally (not sure if
> I described it correctly) so that every call of function/method in web
> application first will call custom function before and put information's
> from console.count. In that way I could measure how many times every
> function/method is called. That's my goal.

It is possible to wrap all *your* functions (that you *know*) simply by 
replacing them (I do something similar in jsx.dom.addEventListener() in 
order to emulate EventTarget::addEventListener() when unavailable):

  var myFunction = function () {
    console.log("myFunction");
  };

  /** 
   * Adds a call counter to a function.
   * 
   * NOTE: Must pass object reference and property name for a general
   *       solution because there is no call-by-reference
   * 
   * @param {Object} obj
   * @param {String} propName
   * @return {Function}
   *   Decorated function
   */
  function addCounter (obj, propName)
  {
    var original_function = obj[propName];

    if (typeof original_function != "function")
    {
      throw new TypeError(
        "addCounter: obj[propName] must refer to a function");
    }

    var decorated = function () {
      console.count("myFunction");
      original_function.apply(this, arguments);
    };

    obj[propName] = decorated;

    return decorated;
  }

  /* 
   * Example: in global context; cannot generally decorate local functions
   */
  addCounter(this, "myFunction");

However, as I have (re)discovered today thanks to your posting, it is easier 
to use the Firebug profiler.  Go to the “Console” tab, select “Profile”, and 
select “Profile” again when you are done.  The second column of the profile 
shows the number of times each function/method was called.

<https://getfirebug.com/javascript>

However, note that you will be only profiling the behavior of your code with 
Mozilla JavaScript.

I could not find a similar feature in Iceweasel 30’s built-in Web Developer 
Tools or in Chrome Developer Tools in Chromium 34.
 
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Bind custom function to the all Function objects Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-06-13 16:32 +0200
  Re: Bind custom function to the all Function objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-13 16:56 +0200
    Re: Bind custom function to the all Function objects Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-06-13 17:21 +0200
      Re: Bind custom function to the all Function objects "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-06-13 08:39 -0700
      Re: Bind custom function to the all Function objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-13 19:01 +0200
        Re: Bind custom function to the all Function objects Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-06-13 22:41 +0200
          Re: Bind custom function to the all Function objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-14 01:57 +0200
            Re: Bind custom function to the all Function objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-14 15:55 +0200
            Re: Bind custom function to the all Function objects Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-06-16 21:55 +0200
              Re: Bind custom function to the all Function objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-16 23:26 +0200
  Re: Bind custom function to the all Function objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-18 21:04 +0200

csiph-web