Introduction to OO Javascript
JavaScript development can become quite complicated if/when you are dealing with more than just onclick or on mouseover events. If you are working on a client-side-heavy web application with full of dynamic html elements, AJAX calls, validation etc. it is already time to think about OO javascript implementations.
There are various types of javascript OO implementation, the most popular and accepted of which is the employment of the prototype property. By this most of the OO logical properties can be emulated/applied.
MyNamespace.MyClass = function()
{
// Implement the member variables
}
MyNamespace.MyClass.prototype.MyPublicFunction = function(value)
{ this.MyMemberVariable = value; }
MyNamespace.MyClass.MyPublicStaticFunction = function()
{...}
However, the implementation of this type of logic is hard to keep track of, you cannot have a complete overview of your classes, properties and functions which causes your code to become redundant and gets loaded with typos.
Introduction to Script#
In my quest for the most suitable javascript editor which can keep track of the class structures, entities, method implementations, something really interesting came up: Script#. It was described as:
“Script# empowers you with a development methodology and approach that brings software engineering, long term maintainability and scalable development approaches for your Ajax applications, components and frameworks.”
which attracted my attention immediately. As I was reading through the whole story my interest grew. It was a free tool that has “full” integration to Visual Studio which lets the user write C# code, using the accompanying mock libraries for either the DOM or ASP AJAX. The libraries or so-called scriptlets are compiled into javascript on the compile-time and attach to the related web project. The developer then have full access to the Visual Studios features (except for unit-testing) that increases the maintainability of the projects and efficiency.
Also, from the architectural point, the code can have namespaces, types, structures, encapsulation, inheritance, interfaces basically most of the items on the must-have list of an OO language.
On top of the common javascript development, there are other mock libraries that are included to ease ASP AJAX development and also a library if you are working with Silverlight.
Well, script# was the missing piece in full-scale, content-rich asp application development with VS where you actually do not need some other 3rd party tool just to be able to edit the javascript code. (Note: Visual Studio does have intellisense and javascript editor but neither of them is powerful or reliable enough) .
There are of course down sides of the story. With Script# projects, you basically include the sscorlib.dll instead of the mscorlib.dll. This mock library is really elegantly set up to give the developer most of the capabilities in DOM and javascript, however not all .NET classes are implemented (note: the js compiler is pretty powerful but it is still missing the support for generics and implicit variable declarations and so on which ironically are the basics of JavaScript, thinking about it.) The unit tests which I was actually hoping to be able to use (naively enough), are not yet supported but the author promises them to be included soon. (note: script# is still not yet open source, as I understood, there are plans for this).
Rumor has it Microsoft had actually employed this “tool”/framework (however you call it) in some of their projects like Live Messenger, Office Word (?), MSDN Gadgets. In fact, the package has included a library for Live Messenger API and also project template for MSDN Gadgets.
Conclusion
Well, overall, it looks really promising. The library has been just updated a month or two back so let’s cross our fingers for an open source version. Meanwhile, I will be writing couple of more tutorial-like posts on Script#.
Tags: .NET, JavaScript, OOP, Script#, TDD