Visual Studio Intellisense support for parameters in JavaScript

From the perspective of a .NET developer JavaScript lacks of intellisense. Of course, Visual Studio is capable of showing intellisense for defined objects and their members, but when it comes to parameters we face the untyped world. Last year I did a project with a few developers who had to leave their beloved C# world and enter the JavaScript area. Their motivation declined from day to day. So I thought of how to provide them with what they were missing like water in the desert. My approach is based on the facade pattern. It allows to instanciate objects of a type with an instance of the same type, to acts as a wrapper to restore intellisense a JSON object, to be able to load data from a server request into a domain object Here is the code: function Person(data) { /// <summary>Instantiates a new instance of a Person. /// <para> 1. An JSON object containing data.</para> /// <para> 2. A Person object.</para> /// </summary> /// <param name="data" type="object"> /// An object. /// <para>containing data to initialize a person.</para> /// <para>- or -</para> /// <para>of type Person beeing returned by the facade /// to activate intellisense.</para> /// </param> var args, arg, member, lcMember, memberValue; this.firstName = ''; this.lastName = ''; this.fullName = function () { /// <summary> /// Returns the full name of the person. /// </summary> return this.firstName + ' ' + this.lastName; }; //#region Facade pattern to keep intellisense alive... if (arguments !== null || arguments.length === 1) { args = arguments; arg = args[0]; if (arg instanceof Person) { return arg; } else if (arg instanceof Object) { for (member in arg) { lcMember = member.substring(0,1).toLowerCase() + member.substring(1); if (this.hasOwnProperty(lcMember)) { memberValue = arg[lcMember]; if (arg.hasOwnProperty(lcMember)) { memberValue = arg[lcMember]; } else if (arg.hasOwnProperty(member)) { memberValue = arg[member]; } if (typeof(memberValue) === 'string' && memberValue.indexOf('/Date(') === 0) { memberValue = new Date( parseInt( memberValue.substr(6))); } this[lcMember] = memberValue; } } } } //#endregion return this; } The only thing I don’t like about is the fact that I need to name the type when comparing by instanceOf… Wrapped in an Visual Studio snippet eases up the pain… And here is an example of the usage: $(document).ready( function () { // After an object is instantiated intellisense works... var person = new Person({ firstName: 'daniel', lastName: 'fisher' }); // ... but when passed as a parameter it gets lost ... printFullName(person); }); function printFullName(person) { // ... using the facade pattern we let intellisense // know what *type* it is. var thePerson = new Person(person); console.log(thePerson.fullName()); } I hope this helps!

Chaining asynchronous javascript calls

The good thing about JavaScript is that its network/ajax calls are asynchronous. The bad thing about JavaScript is that its network/ajax calls are asynchronous. It’s bad because nested calls increase complexity. Look at the following sample methods: function f1(callback) { console.log('f1'); if (callback != null) { console.log('hasCallback'); callback(); } } function f2(callback) { console.log('f2'); if (callback != null) { console.log('hasCallback'); callback(); } } function f3(callback) { console.log('f3'); if (callback != null) { console.log('hasCallback'); callback(); } } The calls should be executed one after another. f3( function(){ return f2( function() { return f3(); } ); } ); Reading or even worst writing this makes me not happy. I don’t like the explicit nesting. It makes the code feeling complicate. It often don’t fits in the frame of 80 chars per line . Today I’ve put together a small helper class that collects calls and executes them by chaining them as callbacks. var devcoach = devcoach || {}; devcoach.CallChain = function () { var cs = []; this.add = function (call) { cs.push(call); }; this.execute = function () { var wrap = function (call, callback) { return function () { call(callback); }; }; for (var i = cs.length-1; i > -1; i--) { cs[i] = wrap( cs[i], i < cs.length - 1 ? cs[i + 1] : null); } cs[0](); }; }; This few lines now let you write a more clear and easy to read calls automatically being nested calling the follower as callback. var cc = new devcoach.CallChain(); cc.add(f1); cc.add(f2); cc.add(f3); cc.execute();

ICE Lingen 2011

It’s community summer again. The same procedure as every year. In the middle there is some refreshing ICE. The ICE-Conference in Lingen is something special to me because it is an admin event. I’m a developer. Maybe It’s caused by my roots – yeh, I was an admin once. Maybe caused by the fact that I tend to build bridges between admins and devs and build my developments aware of infrastructure. This year I spoke about professional development on the client stack. I explained Javascripts OOP concepts from the .NET development perspective. Introduced JQuery and explained how it’s plugin model works.  The session was rated along with others as 3rd bets of the conference – Thanks for attending and voting! As always the networking with the *admin guys* was as great as the after-ice-party. I really enjoyed to meet up with a lot of people I’ve haven’t seen for a while. Last but not least: Many thanks to Nicki Wruck and the ICE-Team for this great event.