JavaScript open my eyes (1) digg delicious su

Posted by tiendung
on Wednesday, October 10

Don’t be fool by the title, it’s used to attract readers like you. This series is about programming in general. But wait, I’ll talk about JavaScript later because it’s true that JavaScript is cool and it has changed my programming point of view.

Before know JavaScript, I lived in the world of produce and object-oriented language like Pascal, C/C++ and never know what programming is. In high school, I was taught algorithms before programming languages, just used languages as tools to implement algorithms. Fist and second years in university, I was taught a lot of math before knowing that math is the most important foundation of programming. I wondered why I had to learn math, I were there to be a developer (and I forgot math very soon). Two years later, they taught me about software engineering, database, AI … without showing me how programs actually implemented and run in a computer. As time go by, I write more and more programs and still don’t know about programming. May be this the reason why Vietnam do not have talent programmers. Singapore is better. I found some interesting programming courses at NUS like

CS1101S: Programming Methodology (with Scheme)

CS2104: Programming Language Concepts

CS3212: Programming Languages

CS4212: Compiler Design

But most of other courses related to Java and some courses force student using Java (or not encourage using other languages) to do assignments. For most students in NUS, Java is their language of choice. May be I’m lucky that I know only C and little C++ when I came to NUS and I hated Java at the first sight because it dismisses the main() function and force me to write a class, then create an object just to write a pure object-oriented “hello world” program (I used Java once to do assignments).

Then I tried some dynamic programming languages: Python, Ruby (on Rails) and JavaScript. I didn’t plan to learn JavaScript in order to create web apps because I thought I can do sexy Ajax things via RJS in Rails. But everything is not as expected. In my first project, I have to generate DOM elements from data in JSON format and RJS didn’t help much. I had to use JavaScript and started to learn the language. Ok, it’s just another languages, just learn the syntax and done. My first book is Pro JavaScript by John Resig (the creator of jQuery). He taught me how different JavaScript is from traditional object-oriented programming languages and the private and privileged techniques of Crockford to make object-oriented JavaScript more powerful. These techniques looked very strange, I never saw them before. I stopped reading and try to implement it in my first project, I also used jQuery to handle Ajax calls, DOM … (John mentioned jQuery in his book, of course). Then Crockford JavaScript videos keep my eyes opening (wider and wider) ...

fun with JavaScript Chainability digg delicious su

Posted by tiendung
on Friday, August 10

want to show all string properties of an object in JavaScript?


function showAllStringPropertiesOf( object ) {
  for (var property in object) {
    if (typeof object[property] == "string") {
       console.log(property, ":", object[property]);
    }
  }
}

var object1 = {
      name: "Spiragram",
      address: "87 Beach Road",
      vision: "Spiragram will liberate, lead and inspire programmers to realize their full potential ..." 
    },
    object2 = {
      name: "Tien Dung",
      age: 26,
      saySomething: function () { alert("hello from " + this.name); }
     };

showAllStringPropertiesOf(object1);
showAllStringPropertiesOf(object2);

is it nice if we do not have to duplicate the function name? We can write:


forEach([object1, object2], showAllStringPropertiesOf );
 

where forEach is a funtion:


function forEach( a, fn ) {
  var i, l = a.length;
  for (var i = 0; i < l; i++)
    fn(a[i]);
};

is there a better way (because we have only two objects)? The answer is making the function chainable by adding a return itself command to the end of the function.


function showAllStringPropertiesOf ( ... ) {
  ...
  ...
  return arguments.callee;
}

now we can call:

showAllStringPropertiesOf( object1 )( object2 )

still want to make it more readable? Let make more fun with “and” keyword.

showAllStringPropertiesOf( object1 ).and ( object2 );

by adding some code below

function showAllStringPropertiesOf ( ... ) {
  ...
  ...
  return addAndMagicTo(arguments.callee);
}

function addAndMagicTo( fn ) {
  fn.and = fn;
  return fn;
}

Tien Dung

Rails plugin to combine CSS and Javascript files digg delicious su

Posted by jason
on Friday, July 27

While multiple CSS and Javascript files aid manageability and facilitate a modular style of development, HTTP performance takes a hit because multiple requests are sent to the server for each of these files.

When we first launched HBO Asia’s recently revamped site, we found that the response time for pages to load was somewhat slow and thought that the many separated Javascript and CSS assets we were using might be a contributing factor.

David Heinemeier Hansson’s keynote at Railsconf 2007 mentioned that the upcoming Rails 2.0 will have a feature to combine all Javascript and CSS assets to help with this problem. In the meantime, however, we made do by manually combining the assets.

True enough, after doing so, the site’s performance was a little better. However, maintainability was compromised and every time I updated the combined files, I would have to update the individual files as well (as other parts of the site that do not need all the assets might be referencing the individual files).

Tired of such unproductivity, I decided to search for a Rails plugin that can automate this process and found CachedAssets. I’m currently testing this on development and will push this out to production soon if all goes well.