Wednesday 17 July 2013

Javascript Variable Type Checking

Javascript likes many other scripting languages, variable is dynamically typed. That is great during coding but may not be so great when you run into problem during runtime. So what programmer end up doing is to write his own variable type checking code. Generally this is done at the beginning of every function.
function addAB(a,b){
   var a = a || ""; 
   var b = b || "";
   if (!(typeof(a) === number ||
         typeof(b) === number)) return NaN;

   return (a+b);
}

I always feel bored or tired when writing this kind of code. How about you?
Do you guys have some other tricks/ideas to deal with this problem? Why can't scripting language support dynamic typed variable without letting application/us do all these boring work? If you were to change javascript interpreter to understand variable "a" and "b" are numbers and return error if it is anything else, how would it looks like? Here are some ideas:
function addAB(a=>number,b=>number){..}

function addAB(int a, int b){..} // like good old C function

@type a = number
@type b = number
function addAB(a,b){..} // 'notation' in Java/POJO world

var addAB(int, int) = function(a,b){..}

Other ideas? It will be nice if Javascript can elegantly solve or ease this problem.

Saturday 14 July 2012

Photo Book iPad



This is how it looks like on iPad Simulator's Safari (with no code change). I can see "turn.js" run nicely on iPad. However as you can see from the screencast, when flipping a page over, the canvas tends to resize and "shadow" effect is missing. Maybe the free version does not fully support iPad. For "fabric.js", apparently it is not design for touch based device (no multi-touch gestures support). Of course "drag and drop" does not work on iPad. Lastly, I also tested the app on my iPad (1st generation), it run smoothly with no resize problem during page flip :)    

Thursday 12 July 2012

Better Photo Book



With "drag and drop" and "delete" of photos, it is now a bit more useful thing :)
Let me know if you like to play around with the code? Happy to share the code on github.

Saturday 7 July 2012

Photo Book App


Looking to build a photo book web app. How easy is it ? I google around and found two useful libraries :
  • turn.js : give you a flip book effect. Support all modern browsers (even IE7 !) + iPhone + chrome on Android. However free version has only limited features.
  • fabric.js : allow you to move/re-size your photos on canvas. Rich features + good performance.
With these two libraries, it is really a matter of plug and play.


Next I will need to support drag-and-drop of photos..