OdeToCode IC Logo

What ASP.NET Developers Should Know About JavaScript

Monday, April 9, 2007

"What ASP.NET Developers Should Know About JavaScript" is the title of a presentation I'll be giving at VSLive! next month.

I don't plan for this to be a talk that features ASP.NET AJAX, or JavaScript data types. Instead, I want to focus on two key concepts I had to discover after I decided that I couldn't ignore client-scripting any longer:

  1. Every JavaScript function is an object.
  2. Every JavaScript function has an execution context.

I think these concepts are keys to understanding modern JavaScript libraries. I hope that driving home these two concepts will let a developer understand what the following code is doing, and why it works. The example is contrived, but if you can understand the following code you can follow many of the JavaScript patterns in use today.

<script src="msajax/MicrosoftAjax.debug.js"></script>
<
input type="button" value="Print!" id="button" />
<
script>

    Type.registerNamespace("Geometry");

    Geometry.Point =
function(x,y)
    {
        
this.x = x;
        
this.y = y;
                
        $addHandler($get(
"button"), 'click',
                   Function.createDelegate(
this, this.print));
    }
    
    Geometry.Point.prototype =
    {
        print:
function()
        {
            alert(
this.x + ' ' + this.y);
        }
    }
          
    Geometry.Point.registerClass(
'Geometry.Point');        
        
    
var p = new Geometry.Point(2,5);
    
</script>

There are lots of ancillary topics in the above code: object notation, prototypical inheritance, and the evils of the global object, just to name a few. However, given just 60 minutes I think that coming to grips with #1 and #2 lays a solid foundation for moving forward in the AJAX jungle.