I'm not too sure where I'm going with the little debugger script yet,
hehe. I'm getting more into JS performance and the debugger will
facilitate a way for me to know a few things like, how much I'm
polluting the global namespace (it requires another script, at the
very top of the doc to document native objects first), performance
throughout the app, etc. Here is what it looks like so far: (warning:
in order to be truly useful, it must be loaded first) ..still a work
in progress, but it might spark some ideas to someone:

/*
 * @brief      :  Let's keep track of browser native objects before we
pollute the global namespace
 * @note       :  The point of this is to see how much we are
polluting the global namespace later on
 * @important  :  This MUST be run before any other JS in order to
effectively finding native functions
 */
var natives = natives || [];
for ( var obj in window ) {
  if( String(obj) != 'natives' ) {
    natives.push(obj);
  }
};

window.startDate = new Date(); // we'll use this to test performance

/*
 * In case we forget to remove console traces, create a dummy one for
IE and similar browsers.
 */
if( typeof window.console === "undefined" ){
  window.console={};
  var i,max,
funcs=['log','debug','table','info','warn','error','assert','dir','dirxml','trace','group','groupCollapsed','groupEnd','time','timeEnd','profile','profileEnd','count'];
  for(i=0, max=funcs.length; i<max; i++){
    console[funcs[i]] = function(){}
  }
};


(function (window, undefined) {

  var document = window.document;

  /*
   * @readme : DO NOT USE debug() in the firebug console. The firebug
scope has a
   * convenience function of the same name. Instead use _debug()
   *
   * @references:
   *    + 
http://groups.google.com/group/jsmentors/browse_thread/thread/806bdc92546445a0?hl=en
   *    + http://getfirebug.com/wiki/index.php/Command_Line_API#debug.28fn.29
   */
  function Debugger() {
    this._Debugger = this;
  }


    Debugger.prototype.log = function(data) {
      console.log(data);
    }


    Debugger.prototype.table = function(data) {
      console.table(data);
    }


    /*
     * Our storage container for data and stuff
     */
    Debugger.prototype.data = {
      perf : []
    }


    /*
     * Prints in Firebug a table with performance timings
     */
    Debugger.prototype.perf = function() {
      console.table(this.data.perf)
    }


    /*
     * We'll be storing useful data to later check on performance, etc
     */
    Debugger.prototype.store = function(type, data, date) {
      //For now all times are calculated from the moment startDate was
set
      var time = Math.abs( date - startDate ) + "ms";

      //add the time to the table array
      data.push(time);

      //store it
      this.data[type].push(data)
    }

    Debugger.prototype.on = function() {
      document.cookie = "debug=true;";
    };

    Debugger.prototype.off = function() {
      document.cookie = "debug=false;";
    };

    /*
     *
     * @credit: http://www.w3schools.com/JS/js_cookies.asp
     */
    Debugger.prototype.mode = function() {
      var i,x,y,
          cookies = document.cookie.split(";");

      for (i=0;i<cookies.length;i++) {
        x = cookies[i].substr(0,cookies[i].indexOf("="));
        y = cookies[i].substr(cookies[i].indexOf("=")+1);
        x = x.replace(/^\s+|\s+$/g,"");
        if (x=='debug') {
          return true;
        }
      }
      return false;

    };


  // Store in the global namespace as "debug" for the App and "_debug"
for firebug
  window.debug = window._debug = new Debugger();

  debug.store( 'perf', ['Debugging Tools', 'Time it took to execute
first debugging tools'], new Date() );

})(window);



On Jul 5, 5:10 am, David Marrs <[email protected]> wrote:
> >> 2011/7/5 David Marrs <[email protected]>:
> >> > Why do you have a 2nd arg in your lambda called undefined?
> >> Because he might want to use the undefined value, and in case someone
> >> in the global scope created a variable named undefined, then it can
> >> cause trouble.
>
> > If in doubt, use "void 0" instead. That always evaluates to undefined.
> > /L
>
> I think I prefer this option.  I can see someone accidentally
> redefining the closure's undefined by passing in an arg by mistake.
> That could be a difficult bug to spot as well.

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to