Monday, May 26, 2008

Javascript in 600 Seconds

Breakdown...

Basic Types

var intValue = 1;
var floatValue = 3.0;
var stringValue = "This is a string\n";
var sqString = 'This is also a string';

Javascript is a dynamically typed language. Variables are declared with the keyword var. Common simple types are supported.

Arrays

var emptyList = [];
var homogenousList = [1, 2, 3];
var heterogenousList = ["one", 2, 3.0];

Javascript has built-in collection objects. The Array object is a dynamically typed sequence of Javascript values. They are created with the bracket notation [] or with the new operator on the Array object (e.g. new Array(5)).

Property Maps

var emptyMap = {};
var homogenousMap = {"one": 1, "two": 2, "three": 3};
var heterogenousMap = {"one": 1,
"two": "two",
"three": 3.0};

Along with Arrays are the Object objects. They act as property maps with strings serving as keys to dynamically typed data.

Access

// Dot notation property access
window.alert("Homogenous map property \"one\" "
+ homogenousMap.one);
// Subscript notation property access
window.alert("Homogenous map property \"two\" "
+ homogenousMap["two"]);

Assignment

homogenousMap["one"] = 10;
homogenousMap.two = 20;

Removal

delete homogenousMap["one"];
delete homogenousMap.two;

Iteration

for (var key in heterogenousMap) {
window.alert("Heterogenous map property \""
+ key
+ "\" = "
+ heterogenousMap[key]);
}

Functions

var callable = function (message) { // <-- notice assignment
window.alert("Callable called with message = "
+ message);
}


function createClosure(initial) {
var res = function () {
initial = initial + 1;
window.alert("Closure with modified state "
+ initial);
}
return res;
}

function callCallable(f, x) {
f(x);
}

function composeCallables(f, g, x) {
f(g(x));
}

Functions are first-class objects. That means that they can be created dynamically, stored, passed and returned just like any other value.

Objects

function MyObject(name, value) {
this.name = name;
this.value = value;
}

Javascript supports prototype based object orientation. Not a class type but an object constructor is created for new objects with particular properties. In the example above the this keyword used to reference the ''current instance'' of the object. The this object is essentially a property map with members accessed (and initialized) in this example with the dot notation.

The object constructor, MyObject, is an object constructor not in how it's defined, which looks like any other Javascript function, but in how it's ''invoked''.

    var my = new MyObject("foo", 5);

The new operator before the function invokes the function with a newly construced object as this and returns that the initialized object.

Object Prototype

Part of what makes a language object oriented is that data not only has properties but also ''behaviors''. Also known as: member functions; methods; and object messages. To implement a member function in Javascript one would be tempted to write something like what's below based on the member initialization exampled above.

function BadObject(data) {
this.data = data
this.memberFunction = function () {
// ...functions on data...
}
}

While the code above will work without error, it does create a new closure for each member function for each new instance of the object. What's really required is a class level function that works on instance data. But remember, Javascript objects aren't class based but prototype based. So how do we implement "class" level member functions? (Skip to Implementation) Better yet, how do we implement "class" level members functions in general?

Enter the prototype member.

The internal object member, prototype, has language defined significance in that it is used for resolving property names if the property isn't found in the current property map. It's considered internal because, while the instance's prototype member is ''inherited'' from the ''constructor's'' prototype member, it cannot be accessed directly from the object instance itself. The defined prototype member is a property map itself which holds members for property name resolution. Consider the example below:

 var parentPropertyMap = {"bar": "I'm the bar"};

// Define the constructor with inheritable properties
function ChildObject(foo) {
this.foo = foo;
}
ChildObject.prototype = parentPropertyMap;

childPropertyMap1 = new ChildObject("I'm the foo1");
childPropertyMap2 = new ChildObject("I'm the foo2");

// Prints "childPropertyMap1.foo = I'm the foo1"
window.alert("childPropertyMap1.foo = "
+ childPropertyMap1.foo);

// Prints "childPropertyMap2.foo = I'm the foo2"
window.alert("childPropertyMap2.foo = "
+ childPropertyMap2.foo);

// Prints "childPropertyMap1.bar = I'm the bar"
window.alert("childPropertyMap1.bar = "
+ childPropertyMap1.bar);

// Prints "childPropertyMap2.bar = I'm the bar"
window.alert("childPropertyMap2.bar = "
+ childPropertyMap2.bar);

The member foo is an instance member added to the instance's property map during construction:

 function ChildObject(foo) {
this.foo = foo;
}

while bar is in the constructor's prototype:

 var parentPropertyMap = {"bar": "I'm the bar"};
...
ChildObject.prototype = parentPropertyMap;

which is ''inherited'' during the new operation:

 childPropertyMap1 = new ChildObject("I'm the foo1");
childPropertyMap2 = new ChildObject("I'm the foo2");

In other words, the member, bar, is shared across all instances of ChildObject.

Therefore, by implementing the prototype member of the constructor function, we can think of the constructor function itself as the "class" object. Complete with static class functions:

 function ClassObject() {}
ClassObject.staticClassFunction = function(x) {
return x * 2;
}

static class variables:

 function ClassObject() {}
ClassObject.staticClassVariable = 5;

shared member variables:

 function ClassObject() {}
ClassObject.prototype.sharedMember = 5;

and of course, shared member functions:

 function ClassObject(x) {
this.x = x;
}
ClassObject.prototype.memberFunction = function(x) {
return x * this.x;
}

Member Function Implementation

function Message(message) {
this.message = message;
}

Message.prototype.show = function() {
window.alert("Message.show() with message = "
+ this.message);
}


Example Code

//////////////////////////////////////
// Basic Types
var intValue = 1;
var floatValue = 3.0;
var stringValue = "This is a string\n";

///////////////////////////////////////
// Array
var emptyList = [];
var homogenousList = [1, 2, 3];
var heterogenousList = ["one", 2, 3.0];

///////////////////////////////////////
// Property Map
//
var emptyMap = {};
var homogenousMap = {"one": 1, "two": 2, "three": 3};
var heterogenousMap = {"one": 1,
"two": "two",
"three": 3.0};

///////////////////////////////////////
// Functions as values
//
var callable = function (message) { // <-- notice assignment
window.alert("Callable called with message = "
+ message);
}

function createClosure(initial) {
var res = function () {
initial = initial + 1;
window.alert("Closure with modified state "
+ initial);
}
return res;
}

///////////////////////////////////////
// Functions as arguments
//
function callCallable(f, x) {
f(x);
}

function composeCallables(f, g, x) {
f(g(x));
}

///////////////////////////////////////
// Objects
//
function MyObject(name, value) {
this.name = name;
this.value = value;
}

///////////////////////////////////////
// Objects with Member Functions
//
function Message(message) {
this.message = message;
}

Message.prototype.show = function() {
window.alert("Message.show() with message = "
+ this.message);
}

///////////////////////////////////////
// Demo Utilities
//
function quote(message) {
return "\"" + message + "\"";
}

///////////////////////////////////////
// HTML Invoked demonstration
//
//
function main() {
window.alert("Integer = " + intValue);
window.alert("Float = " + floatValue);
window.alert("String = " + stringValue);

for (var item in emptyList) {
window.alert("Empty list item = " + item);
}

// Script style index iteration
for (var i in homogenousList) {
window.alert("Homogenous list item = "
+ homogenousList[i]);
}

// C style index iteration
for (var i=0; i < heterogenousList.length; ++i) {
window.alert("Heterogenous list item = "
+ heterogenousList[i]);
}

// Dot notation property access
window.alert("Homogenous map property \"one\" "
+ homogenousMap.one);
// Subscript notation property access
window.alert("Homogenous map property \"two\" "
+ homogenousMap["two"]);

for (var key in heterogenousMap) {
window.alert("Heterogenous map property \""
+ key
+ "\" = "
+ heterogenousMap[key]);
}

callable("(Function value invoked)");
closure();
closure();

callCallable(closure);
composeCallables(callable, quote, "My Message");

var my = new MyObject("foo", 5);
window.alert("MyObject my.name = " + my.name);
window.alert("MyObject my[\"value\"] = " + my["value"]);

var msg = new Message("bar");
for (var key in Message.prototype) {
window.alert("Message prototype member \""
+ key
+ "\" = "
+ Message.prototype[key]);
}

window.alert("Message msg.message = " + msg.message);
msg.show();
}

Friday, May 23, 2008

40 Unique Resources for JavaScript Coders

Are you an advanced JavaScript coder looking for more sites to sharpen your coding prowess? Maybe you’re a web designer wanting to double as a developer (or at least know enough to add a bit of rich content into your designs). Either way, if you’re looking for more information on the topic of JavaScript, the following resources are worth a gander.

Reference, Resources, & Tutorials

DevGuruDevGuru - JavaScript Quick Reference

DevGuru provides an extensive list of JavaScript syntax, alphabetized similar to a glossary for easy scanning and searching.

TechCheatSheets.com - Javascript Cheat SheetsTechCheatSheets.com - Javascript Cheat Sheets

A roundup of 10 JavaScript cheat sheets in one place; includes cheatsheets for frameworks such as jQuery and Prototype.

Google Groups - comp.lang.javascriptGoogle Groups - comp.lang.javascript

If you’re looking for a community of JavaScript’ers comp.lang.javascript is an active and helpful community of developers.

jQuery for DesignersjQuery for Designers

jQuery for Designers is geared towards designers who want to learn about the jQuery library to add more dynamic content in their designs.

Freetechbooks.com - Free Online JavaScript BooksFreetechbooks.com - Free Online JavaScript Books

In this collection, you’ll be able to download 5 excellent e-books on the topic of JavaScript, all for free.

DZoneDZone

Although not purely a JavaScript resource, DZone regularly features articles, tutorials, resources, and news about JavaScript.

W3Schools - JavaScript TutorialW3Schools - JavaScript Tutorial

W3School’s section on JavaScript offers beginning to advanced JavaScript topics.

15 Days Of jQuery15 Days Of jQuery

Straight off the home page, 15 Days of jQuery has "Fantastic tutorials and example code that takes you from zero to hero in no time flat".

The "Mootorial"The "Mootorial"

//clientside’s tutorial on the mootools framework has a built-in console for you to try out JS code.

Premade Scripts/Code

AjaxDaddyAjaxDaddy

A collection of downloadable DHTML scripts. AjaxDaddy provides a demo for the featured scripts.

MiniAjax.comMiniAjax.com

Another site with a collection of DHTML and Ajax code, similar to AjaxDaddy.

JavaScript KitJavaScript Kit

Here, you’ll find downloadable scripts, as well as tutorials and guides on JavaScript.

Dynamic Drive JavaScript code libraryDynamic Drive JavaScript code library

DHTML scripts organized into 16 categories including Calendars, Image Effects, Links & Tooltips, and more.

DHTMLgoodies.comDHTMLgoodies.com

Yet another place to get your fix of DHTML/Ajax scripts. They also have a fairly nice and straight-forward Ajax basics tutorial.

4umi useful Javascript4umi useful Javascript

A "database" of useful scripts and code snipplets that are updated fairly often.

Articles & Blog Posts

The Most Complete AJAX Framework and JavaScript Libraries List(124+)The Most Complete AJAX Framework and JavaScript Libraries List(124+)

The title pretty much says it all — it’s a huge list of JS frameworks/libraries.

The seven rules of unobtrusive JavaScriptThe seven rules of unobtrusive JavaScript

This excellent article outlines seven things to keep in mind when trying to develop unobtrusive JavaScript solutions.

How simple is making your javascript unobtrusive? Easy as PieHow simple is making your javascript unobtrusive? Easy as Pie.

A basic introductory article on "unobtrusive JavaScript.

The Top 40 Free Ajax & Javascript Code for Web DesignersThe Top 40 Free Ajax & Javascript Code for Web Designers

A list of scripts geared towards web designers (i.e. not a lot of manual coding involved).

How to choose a JavaScript frameworkHow to choose a JavaScript framework

Outlines a few considerations when deciding which JS framework is right for you.

Efficient JavaScriptEfficient JavaScript

An article on quick tips for optimizing your JavaScript code.

Ten Javascript Tools Everyone Should HaveTen Javascript Tools Everyone Should Have

A list of JS code snipplets recommended to have in your coding arsenal; among them are numeric sorting and working with cookies.

Serving JavaScript FastServing JavaScript Fast

Optimal tips for serving/loading your JavaScript libraries quickly.

The Great Browser JavaScript ShowdownThe Great Browser JavaScript Showdown

A comparison of the top 4 web browsers (IE7, Firefox 2, Safari 3.0.4, and Opera 9.5) when it comes to handling JS.

Quick guide to somewhat advanced JavaScriptQuick guide to somewhat advanced JavaScript

A guide on Object-Oriented JavaScript coding.

Blogs & News

John Resig - BlogJohn Resig - Blog

John Resig is the creator/lead developer of jQuery and author of "Pro Javascript Techniques".

Ajaxian - JavaScript> JavaScript">Ajaxian - JavaScript

Ajaxian is a news site about Ajax and Rich Internet Applications. Over 850 stories have been tagged under the JavaScript topic.

Snook.CA - JavaScript CategorySnook.CA - JavaScript Category

Snook.CA is Johnathan Snook’s site on the topic of web development. He writes about JavaScript, as well as other web dev topics.

AjaxlinesAjaxlines

Ajaxlines provides news and resources on the topic of Ajax. It currently has 140+ posts tagged under JavaScript.

QuirksBlogQuirksBlog

QuirksBlog is part of JavaScript guru/web developer Peter-Paul Koch’s QuirksMode.org. His book ppk on JavaScript is an excellent book to own.

Ajaxonomy - BlogsAjaxonomy - Blogs

Ajaxonomy is a wonderful resource for JavaScript’ers interested in Ajax and other web technologies. It has many posts tagged with JavaScript.

Ajax Bestiary - A JavaScript Field GuideAjax Bestiary - A JavaScript Field Guide

Ajax Bestiary is a regularly updated blog on JavaScript.

Awesome Frameworks/Libraries

Prototype JavaScript frameworkPrototype JavaScript framework

Prototype was one of the first popular frameworks. Several libraries and frameworks are based on Prototype (or still require it).

jQueryjQuery

jQuery is lightweight, elegant, and touted as one of the easiest JS frameworks to use.

mootoolsmootools

My personal favorite.

The Yahoo! User Interface Library (YUI)The Yahoo! User Interface Library (YUI)

A big and extremely robust JavaScript toolkit by Yahoo!.

JavaScriptMVCJavaScriptMVC

JavaScriptMVC is a relatively new but very promising framework that offers a lot of unique components and features not found in other frameworks.

script.aculo.usscript.aculo.us

A robust effects library that’s been used by top websites such as Digg, Feedburner, and Apple; requires the inclusion of Prototype.

Ext JSExt JS

Another solid framework; it does have a restrictive license for commercial purposes. Check out the Web Desktop demo.

MochiKitMochiKit

MochiKit is a robust library that offers a lot of utility functions and effects classes.

DojoDojo

Dojo is another framework to consider. visit the Spotlight section on the website to see real companies using Dojo.

Additional Resources

Author’s note - April 16, 2008. After the publication of this article, I recieved several recommendations from readers about additional resources that didn’t make the list. I’d like to add even more excellent JavaScript resources worth taking a look at. I’ll add more as suggestions come along.



Ref: http://sixrevisions.com/resources/40_resources_for_javascript_coders/

Friday, March 14, 2008

The future is waiting for Linux

The rising generation of programmers isn't being fed .Net and Windows. It's growing strong on Linux and its associated LAMP stack, as Robert Guth of the Wall Street Journal notes. Microsoft thinks it has an answer to this trend toward Linux. It is very telling how far from reality Microsoft is by its response:

Microsoft hasn't been a player in the Net start-up world, in part because of the cost of its server product. Mr. Hilf tells [the WSJ] that Microsoft is trying to fix that with new licensing schemes that make Windows Server more affordable for start-ups....

The technology has also been a hindrance, which Mr. Hilf says Microsoft tried to overcome by making additions to Windows Server 2008 that might appeal to Linux programmers who want better access to the technical guts of the software. Such changes "will be a big impact to that next-generation Facebook," Mr. Hilf says.

Well, no, Bill. Such changes are largely irrelevant at this point. You've already lost the mindshare war, and tepid changes to Microsoft's server licensing policies won't change things, either. Your company's limp olive branch to the open-source community ("You can use our software royalty-free and without fear of legal retribution...so long as you never make a penny from your efforts") is worse than insulting.

Microsoft's model is perfect for the client/server model that it helped to pioneer. It is irrelevant for the web-enabled future that is being built even as I type. This new world looks more like Firefox: platform agnostic. It doesn't care if people run Windows. Neither should you.

Ref : http://www.cnet.com/8301-13505_1-9892174-16.html