Follow @RoyOsherove on Twitter

Script# (Script Sharp) – writing javascript in C#

I’ve started dabbling with web development. I’m building a secret project that I’ll unleash one day. soon.

I’m learning a lot of JS and JQUery foo as I’m putting together a coupld of ajax and interaction intensive pages on the site.

The javascript experience has been not so great. While it’s a piece of cake to get small scripts going, once you get beyond a couple of pages long script files, you get into a nightmarish scenario where navigating, refactoring or sometimes understanding the relationships in your javascript code becomes slow to nearly impossible.

there’s a lot to be said for the open way in which javascript works. but it’s too “open” in terms of programming experience.

So I looked at way to arrange javascript better. from ways to arrange files and classes, to loading JS dynamically. it all pretty much sucks.

I can’t believe this is the state of the art after a few good years of web 2.0 programming. really.

Enter Script#.

This thing has potential to be huge.

It’s a custom compiler that sits inside visual studio and gives you “special” projects that you write like C# libraries. the code that you write then gets compiled to both a dll (which you don’t care about) and to a javascrip file (in two versions – a minified version and a debuggable version).

it’s basically a translation of the C# code you write into javascript.

this allows me to write a whole bunch of very interactive scripts, but they are easily managed like regular C# files and projects.

  • I can refactor the code easily
  • I can navigate easily
  • there’s a representation of the document and script object models has C# frameworks, so I get intellisense
  • I can also use JQuery and other things which I depend on (like jquery plugins etc..)

the only thing you don’t get is the debuggability. for that you still need to debug javascript code, which the generated code is very readable.

  • It does not support generics yet, which is OK. You don’t need generics to do great stuff in javascript code. but it would be nice to have!
  • It does not support JQuery out of the box. there is a solution, and the end of this post.
  • It still has some quirks and things you need to learn using the script objectmodel so that you get what you intend in your javascript code.

overall, despise the very different nature of C# and JS, I find that with Script# I almost manage to have it all: Great javascript interaction, but a “managed” experience when writing that code.

How can I get it?

Including JQuery into the mix

  • I downloaded and extended the JQuery for Script# project. it’s very easy to “get” and get going once you grokked Script#.
  • The “$” sign is out. You can’t use the “$” sign since it’s not allowed in C#. so the generated code actually uses it in other places in the generated code (it was not built with jQuery in mind). So if you have other non generated JS code that uses jQUery you’ll need to use the “safe” version of jQuery (“$(‘something’)” becomes “jQuery(‘something’)” )
  • you will need to add any plugins jQuery uses on top of that. it’s very simple:

The thing you need to realize about “supporting” other frameworks, is that when you code in C# you need to think at a different level - “what will be generated from this?”. There are two kinds of code in script# –

  • there’s “regular” code which you write which gets all translated into javascript.
  • and then there’s “Exported” code, which is just a strongly typed “shell” you can program against, but does not really get generated on the JS side. You note that code by putting a couple of attributes on the class.

for example, the JQuery additions to Script# are just one class called jQUery with methods that have the same signatures as methods in the jQuery object model, but with no implementation but the simplest one to make them compile. any calls to this class will be generated, but the class being called will not be generated. You are in fact saying to the compiler ‘expect that there’s already an existing object model I will work against" – ignore this class when you generate code”. very cool stuff.

 

Things I’d love to see in Script#:

  • More frequent updates and .net 3.5 support.
  • Features: I’d love to be able to use “new {Prop=”value”}” anonymous types to generated JSON objects. also I’d love to have lambda support instead of just a simple delegate support.
  • Open source it baby. so we can all contribute and learn from it.

No Excuses

Webinar: Driving unit testing in the organization