Javascript Function Body Equality Checking

If you need to check to see if two functions are the same instance, you can just check the variables for equality:

 

var f = new function() { 
  alert("hello world");
}; 

var f1 = new f();
var f2 = new f();
var boolTest = (f1 == f2);

 

However, if you need to check to see if the bodies of those functions are equal (or both contain some text, etc -- essentially, just working with the actual language of the function), you can just cast as string to get the text of the function, then check for equivalency:

function fnsAreEqual(f1, f2){
  return String(f1) === String(f2);
} 

var boolTest2 = fnsAreEqual(
  function(){ alert("sameFn"); }, 
  function(){ alert("sameFn"); }
  ); 

updated: fixed JavaScript formatting

Posted on 4/27/2009 7:40:00 AM by Jason Nadal

Permalink | Comments |

Categories: javaScript | performance

Tags:

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Javascript scoping woes

I got re-energized in javascript thanks to a great talk from a colleague today.

I came home and elected to try out my new l33t skillz, and promptly hit a roadblock due to the dreaded scoping of "this".

Basically the scenario is this; I have an object MyObject with a function Reminder(), which gets called after a 10 second delay. The reminder pops up, and when the user closes the reminder, the object's ResetTimer() function gets called. The last line of the ResetTimer function that I was trying to do was:

setInterval( this.Reminder, 10000), and the Reminder function acts on "this". The problem is that when the Reminder function gets called, "this" becomes window. The easy fix, as outlined in this great blog post by Alex Le is to use a closure function, passing the instance into the closure:

setInterval(function(that) { that.Reminder(); }, 10000, this);

The last step was to refactor up to the prototype and all was good!

Posted on 11/14/2008 9:05:00 PM by Jason Nadal

Permalink | Comments |

Categories: development | javaScript

Tags:

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Javascript, AJAX, Controls and Libraries, Oh My.

While looking around the net today for a specific control, it's amazing the number of javascript do-everything libraries that are out there. From mochikit and scriptaculous, to Yahoo.UI and Google's offering, to various ajax libraries like Microsoft's AJAX.NET, AJAX.NET Pro (the non-MS offering), and many many others, the number of choices is amazing. It seems they all have thier shortcomings, but that doesn't stop the devs on these projects to continue their broad scope.

What I came across that was a bit unusual was a framework that relies on a compiler that runs at serve time. Called Jitsu, it's yet another UI, animation, data binder, and AJAX library. The site looks pretty stale (circa 2006), but the approach was interesting because it was different.

Posted on 10/26/2008 4:26:00 PM by Jason Nadal

Permalink | Comments |

Categories: development | javaScript

Tags: , ,

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Javascript, Booleans, and Implicit/Explicit Casting Performance

Ahh the variant. It provides so much ease in writing code, and yet can cause so many headaches. In Javascript, I learned today what joys and pains of assumption of datatypes through boolean expressions can provide. Take the following simple statement:

var x=true;
var y=false;
if (x || y)
  alert('true');

In this case, x is ORed with y, resulting in an obvious true. But we're assuming boolean values. What happens when one has the possibility of not being boolean?

So the creators of javascript saw in their wisdom to add strong-ish typed declarations. You can declare a boolean explicitly, like so:

var isInAGoodState = new Boolean(true);

So all is happy and we can use good booleans. However, there's also an implicit declaration that's much better in terms of performance:

var isInAGoodState = !!(sourceValue);

Essentially saying not-not-sourceValue, using a double negative, results in a true statement. The amazing thing here is just the scale of the performance gain. From a source example, "Overall, the performance gain for using implicit conversion averaged out to 53% across browsers after 10 tests"

Posted on 10/23/2008 7:16:00 PM by Jason Nadal

Permalink | Comments |

Categories: development | javaScript

Tags: ,

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5