Practical ASP.NET

JavaScript for the ASP.NET Developer

Peter has been experiencing culture shock as he adapts to working with JavaScript, starting with the discovery that JavaScript isn't an object-oriented language.

I've always had a jaundiced view of JavaScript (see my column " JavaScript and AJAX in ASP.NET: Not Quite Ready for Prime Time " and the subsequent reader comments). It's always seemed to me that whatever benefits may result from late binding, the inability in JavaScript to specify a datatype for a variable kept the language from being a "real" programming language -- at least for business purposes.

However, as I do more AJAX-style programming, I find myself developing a grudging respect for the language.


Part of the reason that I lacked respect for JavaScript was a fundamental misunderstanding on my part. Since I started programming, I've worked in either procedural or procedural+object-oriented languages. I've dabbled in various other language types (mostly rule-based languages like Prolog and XSLT or declarative languages like SQL) but I've always been true to my object-oriented langauges.

What has re-aligned my worldview is a book on JavaScript by Douglas Crockford called JavaScript: The Good Parts. Crockford's credentials for writing a book on JavaScript are excellent; among other qualifications, he's responsible for the JSON format which is the de facto standard for moving structured data between the client and server in AJAX-enabled applications.

As the negative reviews on Amazon.com point out, the book is short and definitely not focused on teaching you JavaScript (for that, I like David Flanagan's JavaScript: The Definitive Guide, also from O'Reilly Media). Instead, the book is aimed at bringing out the essential concepts of the language -- which I had been ignoring.

So this revelation of material well-understood by all "real" JavaScript programmers is the basis for this column: to describe, in terms that make sense to ASP.NET programmers, the fundamental concepts of JavaScript and how they are different from the object-oriented languages that we're used to.

Objects vs. Prototypes
Unlike the languages I use when writing server-side code, JavaScript is not an object-oriented language. Instead, it's organized around prototypes. In JavaScript you can define data structures and then either use them or copy them. The ability to copy a structure to create a new one means that any structure can be a prototype for subsequent structures. However, unlike a class in object-oriented programming where the definition is inviolable, prototypes or their copies can be extended or modified at any time.

This code, for instance, defines a data structure consisting of two variables: one called id (initialized with a zero length string) and one called dateOrdered (initialized with a Date object). This data structure can be referenced through the variable salesOrder:

var salesOrder = {
    id: "",
    dateOrdered: new Date()
 };

I can start using this structure immediately. This code, for instance, sets the id property to a string value. (And good news! The id property will appear in the IntelliSense drop-down list for salesOrder):

salesOrder.id = "A1230";

However, I can also create copies of this structure by treating it as a prototype. To copy the structure, I create a dummy function, set the function's prototype property to my original structure, then use JavaScript's new keyword to create a new version of the structure from the structure. This code creates a new copy of my structure and references it through a variable called backOrder:

var SalesOrderGenerator = function(){};
SalesOrderGenerator.prototype = salesOrder;
var backOrder = new SalesOrderGenerator;

I can now use this new copy of the data structure:

backOrder.id = "B4567";

If this process isn't different enough, JavaScript allows you to dynamically add a new element to your new copy simply by setting the element to a value. This example adds an outOfStock element to the backOrder version of the structure:

backOrder.outOfStock = true;

All of this is, of course, obvious to experienced JavaScript programmers. But for members of the ASP.NET community who are moving from creating server-side code to creating client-side code (i.e., me), there's a certain amount of culture shock involved.

It's not helpful that a familiar keyword like "new" appears in JavaScript. One of the best rules to follow in user interface design is: "Things that do the same thing should look the same; things that do different things should look different." Running across "new" as a keyword in a different environment initially looks helpful but, in fact, it just encourages me to think of JavaScript as an object-oriented language when it's not.

Still, I'm adjusting. Now, if I could only define a variable as a simple datatype...

About the Author

Peter Vogel is a principal in PH&V Information Services, specializing in ASP.NET development with expertise in SOA, XML, database, and user interface design. His most recent book ("rtfm*") is on writing effective user manuals, and his blog on technical writing can be found at rtfmphvis.blogspot.com.

Reader Comments:

Sat, Jul 4, 2009 Joseph Ghassan www.deepinconcept.com

JavaScript is a loosely typed language. So you should expect all this sort of mismatch between a real language like C# and a scripting language like JavaScript. When JavaScript becomes compiled and the language gets beefed with real OOP paradigm, Javascript will be king! Peter, if you can write more technical articles that would be great. I mean all your articles contains at max 4 lines of code.

Fri, Jul 3, 2009 bruce

about the new operator in javascript. in most languages the new operator is used to create an object and call its constructor. in javascript all functions are really object constructors. so calling a function, creates the object. but in javascript the object constructors have a return value. so a = b(), creates new instance of object b, and sets a to the return value of its constructor, not the instance (which is released as there is no reference to it). if you want a reference to the instance rather than the constructor's return value, then you use the new operator: a = new b() also: var salesOrder = { id: "", dateOrdered: new Date() }; is just syntax shorthand for: var salesOrder = new Object(); salesOrder.id = ""; salesOrder.dateOrdered = new Date();

Thu, Jul 2, 2009

Please update your "About the author" to this: Peter Vogel is a principal in PH&V Information Services, specializing in ASP.NET development with expertise in SOA, XML, database and User Interface design. He has written several books on application development using Microsoft technologies and presents at conferences around the world. He doesn't know much about javascript so take this article with a grain of salt.

Thu, Jul 2, 2009 sam

as an old javascript, .net (vb/asp), C, assembler, crude asp, delphi and object pascal, vb, etc developer... I can really say that javascript is the worst language, with much difference... out there (like lingo and those shits). Even those typical university languages are by far better than this... and years later, its still compulsory to use this shit of ancient language, with all its problems, specially for productivity/business things.

That anonymous say just a true... if you dont know the language, dont talk much about it... but, hell, I can do it for you Peter... Javascript sucks, and will keep sucking until the day is completely replaced by a common, "complete" language

Sat, May 2, 2009 Peter Vogel Canada

I have to admit that I'm still surprised at the reaction that first JavaScript column got. My complaint was that, in JavaScript, the editor let me reference controls that didn't exist and that the problem wouldn't be caught until runtime. Not, in this day and age, an acceptable level of support from an IDE (though, as I pointed out, the IDE's support for debugging was much improved). One astute reader pointed out a useful workaround (embedding server-side code in JavaScript) that addresses the problem but, I have to admit, I'm still hoping for better support in VS 2010. As to my expertise: I'm not a JavaScript expert so, not surprisingly, no one has ever hired me for that (conference or otherwise). However, apparently unlike 'anonymous' I do know something other than JavaScript and that's the expertise my clients hire me for. For which I'm grateful.

Thu, Apr 30, 2009

Guess the only thing "Not Quite Ready for Prime Time" was the author's arrogant attitude in the first place. Hopefully he'll grow out of it as he matures, and next time the first thing he should doubt is his own expertise, rather than a language he's not knowledgeable enough about to comment on.

It's scary that he "presents to conferences around the world"...A sure indicator of lax standards when it comes to booking speakers, one suspects.

Thu, Apr 30, 2009

Wow - a useful, readable, understandable, and thoughtful technical article...
How refreshing!

(Thoughts of Richard Feynman run through my head... :o)

Maybe it hits home with me as my background is (ever so slightly) similar to the author's - but - JavaScript is probably my next challenge - and this article definitely hit the mark for me. Guess I need to go find that "...The Definitive Guide..." book.

thanks

Add Your Comments Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above