Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Tuesday, August 26, 2008

Why JavaScript is important and you will love it :)

Do you know "what is the programming language of the Web?". Is it PHP or ASP
S or C/C++ or Java or Python or Ruby or may be Lisp? (Note: HTML and CSS are not programming languages)

My answer is no, no, no and no. Ladies and gentlemen, our champion is JavaScript:
  • All web browsers have it
  • All Web 2.0 apps use it
  • Adobe Flash & Flex have it
  • It's one of the only four programming languages are allowed to use at Google (others are C/C++, Java and Python)
  • Googlers love it so much that they created a JavaScript web app framework called Rhino on Rails :))
  • It can run on both client side and server side (SpiderMonkey, Rhino ... )
  • Web apps prefer JSON (JavaScript Object Notation) than XML
Now I think you agree with me that JavaScript is important. You should learn it. I'm sure that you will love it when you learn it in the right way.
  • It's a simple language
  • It's easy to learn
  • It's functional
  • It's object is very flexible
  • It supports lexical closure (you can do great thing when combine closure and funtional)
How to learn it in the right what? I suggest you start from here:
http://www.crockford.com/javascript/

More very good and free material can be found at:

Friday, August 22, 2008

Big win for Firefox's SpiderMonkey JavaScript engine

Excellent news from Firefox development team.
JavaScript in Firefox 3.1 is 2 to 30 times faster than in Firefox 3.0. Details here.

It will be a big win any project that use SpiderMonkey engine like Firefox, CouchDB and Aptana Jaxer.

CouchDB use SpiderMonkey for JavaScript function used in views and follow MapReduce model. I had worried about the speed of CouchDB view functions because we all know that JavaScript is much slower than other dynamic programming langugage (Python, Perl ..) because it was treated as a toy language. Now my worry has gone. Thanks to Firefox team who care about JavaScript and performance.

John Resig said that "With this improvement it's leap-frogged any sort of traditional and has gone head-to-head with computationally-powerful languages like C. I fully expect to see more, massive, projects being written in JavaScript. Projects that expect the performance gains that we're starting to see. Applications that are number-heavy (like image manipulation) or object-heavy (like relational object structures). "

Hope it will be true, very soon :)

Wednesday, August 6, 2008

Handling Keyboard Shortcuts in JavaScript

I found this script very useful. Especially when I need to handle key combination (disable default events for "Crtl+P" and "Ctrl+S" for example)

The code work nicely but the source code is not in a good form. So I refactored it:

Check out it and feel free to make it better :)
http://refactormycode.com/codes/402-handling-keyboard-shortcuts-in-javascript#refactor_14545

Tuesday, May 6, 2008

Good jQuery cheat sheets and API lookup

  • www.cheat-sheets.org/saved-copy/Jquery-Cheat-Sheet-1.2.pdf
  • http://labs.colorcharge.com/jquery/jquery12_colorcharge.png
  • http://www.gscottolson.com/weblog/2008/01/11/jquery-cheat-sheet/
  • http://remysharp.com/jquery-api/

Fun with JavaScript Chainability

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 (i = 0; i < l; i+=1) {
fn(a[i]);
}
end

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;
}