For me JavaScript is a wonderful language to develop in. I like how dynamic it is, in terms of creating objects and functions anywhere in the code. The context objects that are used to resolve property lookups, make it wonderful for managing context, with closures and lexical variable scope;
From its roots in scheme it has closures and anonymous functions
which combined with its lexical scope can be incredibly powerful.
Crossbrowser and performance issues can be easily wrapped mostly
because variables can be references to functions, and the appropriate
function for the platform can be returned.
Javascript performance revolves mostly around limiting lookup scope,
this is because a scop chain context is build for each executing
function. to limit this as shown in the video,
http://www.youtube.com/watch?v=mHtdZgou0qU
, using local references to
a global object can greatly reduce the computing time for a variable
lookup.
Here are a few of the features that make javascript a great language to develop in.
// default arguments
function Person(name){
his.name = name || "default";
}
// cross browser window events
function handleMouseEvent(e){
var e = e || window.event;
// code to handle mouse event here
}
// using an object literal for lookup filtering
var acceptable = {'appples':true,'strawberries':true,'lemons':false};
function isAcceptable(opt){
if(acceptable[opt])
return true;
else
return false;
}
// translation maps (great for poorly documented requirements!)
var argMap = {
'red delicious':{name:'apple',type:'fruit'},
'macintosh':{name:'apple',type:'fruit'},
'organic zuccini':{name:'ziccini',type:'vegetable'},
'default':{name:'food',type:'food'}}
function getRealArg(arg)
{
var mapped = argMap[arg];
if(mapped !== undefined)
return mapped;
else
return argMap['default'];
}
// function dispatching
_typefunc['plain'] = function(){
// code here
}
_typefunc['verbose'] = function(){
// code here
}
_typefunc['multi'] = function(){
// code here
}
function makeWidget(obj)
{
obj.run = _typefunc[obj.type];// append function based on type
return obj;
}
Above is a few examples of using the dynamic nature of javascript to quickly compose business logic. Following are some examples of how closures create a context to easily coordinate functionality:
function populate(data, func, chunksize, breakmilli)
{
/* these variables will be in a closure shared by all
* calls to the functions below
*/
var i = 0, len = data.length;
var seglen = Math.ceil(len / chunksize);
var seg = 0;
/* this function will run the loop up until
* the "pos" amount is reached, notice "i" refers
* to the "i" variable above so it will be shared
* bu all calls to "chunkit", the next time chunkit
* is called it will run from the latest value of "i"
* up to pos, the call round to set a timeout for the
* next chunk of values
*/
function chunkit(pos){
while(i < pos){
func(data[i]);
i++;
};
round();
}
function round()
{
if(seg < seglen)
{
seg++;
if(seg == seglen)
var pos = len;
else
var pos = seg * chunksize;
setTimeout(chunkit, breakmilli, pos);
}else{
// actions to take when list is complete
}
}
round();
}
function print(msg)
{
console.log(msg);
}
var data = [1,2,3,4,5,6,7,8,9,10];
populate(data, function(item){
print(item);
}, 3, 1000);
The above code is printing a list of numbers to the console log, three at a time, with a one second break between them. returning control to the user between each set. This technique is very effective, for hiding the perceived time of a task from the user, by returning control to them periodically the application will continue processing other events, such as click events or animations.