Friday, August 22, 2014

Method overloading based on John Resig's addMethod

I was reading the book Secrets of the Javascript Ninga and I came accross the addMethod function by +John Resig.

// addMethod - By John Resig (MIT Licensed)
function addMethod(object, name, fn) {
  var old = object[name];
  object[name] = function() {
  if (fn.length == arguments.length)
    return fn.apply(this, arguments);
  else if (typeof old == 'function')
    return old.apply(this, arguments);
  };
}

  This function keeps a pointer to the old method if it exist (via closure) and overwirtes the method with a new one. This new method compares the function length and the arguments length and if they are equal it calls the method provided otherwise it calls the old method.
  So if you add many methods and try to call the one that was added first, it will go through all the methods added and in each it will fall back to the old method because arguments doesn't match the function length, and finally it will land on the first added.

  I made an alternative to this function.

// addMethod - By Stavros Ioannidis
function addMethod(obj, name, fn) {
  obj[name] = obj[name] || function() {
    // get the cached method with arguments.length arguments
    var method = obj[name].cache[arguments.length];

    // if method exists call it 
    if ( !! method)
      return method.apply(this, arguments);
    else throw new Error("Wrong number of arguments");
  };

  // initialize obj[name].cache
  obj[name].cache = obj[name].cache || {};


  // Check if a method with the same 
  // number of arguments exists  
  if ( !! obj[name].cache[fn.length])
    throw new Error("Cannot define multiple '" + name +
    "' methods with the same number of arguments!");

  // cache the method with fn.length arguments
  obj[name].cache[fn.length] = function() {
   return fn.apply(this, arguments);
  };
}

This 'addMethod' keeps a cache of the added methods using fn.length as the key, and it calls directly the one whose key matches the arguments.length.
I think this might be slightly faster than John's function, but there is a cache property on the method which might create problems if it is altered.

Saturday, March 29, 2014

Where to start

Let me introduce myself.

I am Stavros Ioannidis. A husband, father and programmer.  I am not really a JavaScript beginner. I just feel this way because I learn something new every day. I have built web applications mainly in asp.net with C#, and of course JavaScript.

At first I thought of JavaScript as some language used to do little manipulations on the client and send data back to the server, who is the main entity of an application. I thought of the server as the guy who does all the work even for the UI. Last year I started using JQuery, but again I didn't understand the potential of the library. I was just using the click event. 

Lately I discovered knockoutjs. At first I used it for a profile edit page on a web application I was building at work, and I was really amazed. Maybe I overused it but this way I started to understand how it works. Following the knockoutjs tutorials I discovered sammyjs. In the next application I built I used both sammy and knockout and it was really nice. Of course one day before turning the project in I realized that it was hanging the browser and I had to redo some stuff, but this way I found some performance limits when building applications mainly with JavaScript.

Currently I'm trying to learn some basic JavaScript concepts (prototypes, closures, this etc) so that I understand the language better, and every day get a little closer to becoming a JavaScript pro.

This guy is great. Hellped me a lot.