Chapter 3: JavaScript for the ASP.NET Developer

Chapter 3: JavaScript for the ASP.NET Developer


As an ASP.NET developer, you are used to writing code using a high-level language such as C# or VB.NET. Visual Studio provides Intellisense that makes coding easier. There is rich support for debugging the code, and you get warnings and errors about many things during compilation. Visual Studio will even background-compile your code and give you updated warnings as you go to help you find problems without needing to perform an explicit compilation. The .NET Framework provides a comprehensive set of class libraries that can be used from any .NET-com-patible language and helps to simplify complex programming tasks. Writing JavaScript, however, is somewhat different.

In this chapter, I introduce JavaScript with a bias toward the ASP.NET developer. You will see how JavaScript development is different from what you are used to with .NET languages. In addition, you will see how the browser gets JavaScript. I introduce the basics of the language and explain the closure and prototype aspects of JavaScript coding, along with some techniques to apply for writing faster and more efficient JavaScript. If you look at the code of the Microsoft AJAX Library, you will see that the ASP.NET team uses these tactics in their JavaScript development efforts. If you are already familiar with JavaScript, you may want to skip the first part of this chapter, where I introduce JavaScript, and instead look at some approaches for improving JavaScript code.


This chapter is not meant to be an exhaustive treatment of JavaScript. There are comprehensive books and online tutorials that can take you through the language step by step and highlight differences among the minor version releases from Microsoft, Netscape, and Mozilla. I recommend Professional JavaScript for Web Developers, by Nicholas C. Zakas (2005), or The JavaScript Bible, by Danny Goodman and Michael Morrison (2007), both from Wiley Publishing. The goal here is to highlight the key facts and interesting aspects of the language that may be unfamiliar to someone coming from C# or VB.NET and help you to skip some of the pitfalls that hinder development efforts or slow down JavaScript code.