Friday, December 15, 2006

JavaScript Properties

One of the most annoying limitations in Javascript - or better say in the java syntax, is that it doesn't support the C#\VB.Net property syntax.
So every time you want to create a property, you must create your own getter & setter like the following:


var _myProp;

getMyProp()
{
    return _myProp;
}
setMyProp(value)
{
    _myProp = value;
}


A couple of days ago I found a neat work-around for this limitation.

First, in the constructor, I'll define a span element which will be the host of our object:


function MyObject(id)
{
    this = document.createElement("span");
    this.id = id;
}


Next, I'll create the MyProp method:


this.MyProperty = function(){ return this._myProperty; }


Notice the behaviour of the MyProperty function is of a getter, now all we need to do is define our setter.

In order to do it, we use the span's onpropertychange event:



this.onpropertychange+=setterProxy;

function setterProxy()
{
    switch (event.propertyName)
    case "otherProperty":
        break;
    case "MyProperty":
        /* Here I can call my setter method */
        eval("setter_" + event.propertyName + "()");
        break;
}


And vuala we've got a getter & a setter just like in C#!

You can download the source-code of this example here

Sunday, December 10, 2006

Flexibility Vs. Complexity

One of the features in ASP.Net 2.0 is dynamic compilation - the ability to execute your asp.net code without the need to compile it first.
Instead ASP.Net has a FileSystemWatcher that check for changes in *.cs files and automatically reloads them.

Although this feature is good in many ways, I'd like to focus this post on a specific drawback - design time.

More Flexible Or Too Messy
The dynamic compilation has the same drawback like .Net 3.0's anonymous types do.
Whilst you do get more flexibility to your code\deployment, I find it hard to believe a good design-time support will be - or I dare say can be available.

Even today when your WebSite application becomes bigger than 50~ web-pages, it is very annoying that your Intellisense starts to act funny because you wrote textBox instead of TextBox in one of your classes.

In my opinion, although the wish to extend and to make the language richer and more flexible is good, it can easily make your code too complex to maintain.
A good example to this tradeoff, is the reason why multiple inheritance is not supported in the .Net Framework.

Wednesday, December 6, 2006

Generics - T constructor

A couple of days ago I found an irritating limitation in .Net 2.0 Generics.

Lets say I want to create some sort of a generic object factory, I'll define the following interface:


namespace Sternr.Tests
{
  public interface IGenericFactory
  {
    public T CreateInstance<T>();
  }
}


Where T is the type of object I want to create.
In .Net 1.x, if I wanted to create a new instance of a specified Type, I needed to use reflection, but in 2.0 you can use the following manner:


T newInstance = new T();


Which is prettier ;)
All you need to do to use this syntax, is declare that T has a default constructor:


public T CreateInstance<T>() where T: new()


For now, everythink looks good, but what happens if I want to make my factory smarter - and I want to support a parameterized constructs?
One would think the following should do the trick:


public T CreateInstance<T>() where T: new(object[] args)


But it turns out this syntax is NOT supported and will result in compilation error!

In order to overcome this limitation I had use my last resort - reflection...

Oh well, lets hope it's been fixed in 3.0...

Tuesday, December 5, 2006

Scriptaculous JScript Library

I hope I wont have a lot more of this kind of posts, but I found yet another cool client-side library and I thought it's worth blog about ;)

Scriptaculous is a cross-browser JavaScript library that enables you to really elevate the richness of your UI.

For example, all I need to do in order to make a certain div (or any other html element for that matter) have a phasing effect, I'll simple add the following line of code to the (client-side) onLoad event:


new Effect.Pulsate('theIdOfMyDivElement',{delay:3})


Or maybe you'd like to add a Switching effect to one element when another is clicked?
Here:


<div onclick="new Effect.SwitchOff('otherDivId')">
Click here for otherDivId to switch off!
</div>


Other features available are drag N drop, alert, stretching animation and many more.

Since Blogger is quite difficult when it comes to running jscript code, here's a link to the Scriptaculous demo page

Although most of the library is for creating UI effects, there are more to it, like an Ajax control kit, and DOM utilities - all of which are achieved with minimum scripting.

Sunday, December 3, 2006

Enhancing A Type Functionality

One of C# 3.0 new features is the extension methods - the ability to extend a specific types functionality.

For example, lets say I want to add the string object the GetFirstChar method, that returns the first char of the current string.
Today what I had to do, is create this static method, and call it like this:


string chr = Utils.GetFirstChar(myStr)


In 3.0, by creating the following method:

public static class Extensions
{
  public static int GetFirstChar(this string s)
  {
     return s.Substring(0,1);
  }
}

And now, I could use the following code on any string in my project:


string chr = myStr.GetFirstChar();


JScript Prototyping

Although this is pretty nice, today I found out you could do the same in JScript using the prototype syntax!
Have a look:


String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g,"");
}


And now I can use:


var desc = document.getElementById("txtDescription").value.trim();


Damn, I'm starting to like this scripting stuff ;)