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.


Comments
Rick Strahl Tuesday, April 10, 2007
Join the club. For years I used almost no JavaScript, but recent events have completely changed that. As you point out some of the biggest stumbling blocks when dealing with JavaScript is just how different it is from a language like C# or VB...

I think that's a great topic for a session too - I think there should be more slightly general sessions like these at developer conferences - it's so rare to see this sort of thing at Microsoft based dev conferences these days...
aki Tuesday, April 10, 2007
Could you elaborate this example? What do you need in order to run it? I feel that I am no longer able to avoid javascript but, I don't seem to come to grips with it. What is the essential? Also, what reading do you suggest? Is there a good manual on js to read?
Thanks
Aki
Matt Tuesday, April 10, 2007
I think this would be extremely helpful for developers just beginning to dig into client scripting. It would have helped me overcome some the difficult conceptual hurdles I've encountered over the past 1-2 years.

Javascript has really been an enlightening experience to learn, and, oddly enough, it's making some of the functional programming aspects of C# 3.0 easier to grasp.

Two years ago, if someone mentioned an anonymous delegate creating a closure I would have glazed over. Today, I can instead just whine that my employer chose VB. ;)
Scott Allen Friday, April 13, 2007
Aki: All you need to run the sample is to save the sample to an .html file and run from the hard drive.

The script the sample includes is from the MS AJAX library (http://ajax.asp.net). You can download, install, and then copy this script from a subfolder in the install directory.

As for resources - it's hard. There are a good books, and the best web resource is Douglas Crockford's : http://www.crockford.com/
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!