Thursday, December 29, 2011

Javascript Private Members


Javascript does not have special syntax for private members however they can be implement by using closure in object constructor and using wrap function in object literal. Examples below shows how private members can be implemented.

Example 1. Object Constructor

function Grocery(){
    //private
    var type='fruit';
    
    //public
    this.getType=function(){
        return type;
    }
}
var orange=new Grocery();
alert(orange.type); //undefined
alert(orange.getType()); //fruit

Example 2. Object Literal

var orange;
(function(){
    //private
    var type="fruit";
    
    //public
    orange={
        getType:function(){
            return type;
        }
    };
}());
alert(orange.getType());//fruit

Effect of dependency declaration in Javascript minified code



In the following example although function foo seems longer, but in minified version it is rather smaller than the boo function.This is because compilers will rename local variables name and declaring the dependency at the beginning of the foo function allows smaller code after minification.



function boo() {
    alert(App.namespace.f1);
    alert(App.namespace.f2);
    alert(App.namespace.f3);
}
/*
minified boo
alert(App.namespace.f1);alert(App.namespace.f2);alert(App.namespace.f3)
*/




function foo() {
    var namespace = App.namespace;
    alert(namespace.f1);
    alert(namespace.f2);
    alert(namespace.f3);
}
/*
minified foo 
var a=App.namespace;alert(a.f1);alert(a.f2);alert(a.f3)
*/

Friday, December 23, 2011

Javascript Function Hoisting



There are differences how functions defined in javascript. In javascript all variable regardless of where in the function body declared will get hoisted to the top of function, this also includes  functions which are defined inside the function but there is a difference  how the functions has been declared. the example below shows this difference.


function foo() {

alert('global foo');
}
function bar() {

alert('global bar');
}
function hoistMe() {

console.log(typeof foo); // "function"
console.log(typeof bar); // "undefined"
foo(); // "local foo"
bar(); // TypeError: bar is not a function
// function declaration
function foo() {
alert('local foo');
}
//unnamed function expression
var bar = function () {
alert('local bar');
};
}
hoistMe();

in this example foo will get hoisted to the top with it’s definition but only declaration of bar has hoisted to the top.

First-class object



Is the on which can dynamically be created, destroyed at runtime during the execution of program,and pass as an argument to other functions. In some languages like javascript and python functions are also first class objects.


Ex. Passing function as an argument in Python:


          >>> def op(a,b,fun):
            return fun(a,b)


    >>> def sum(a,b):
            return a+b


    >>> op(1,2,sum)
    3



Ex. Dynamically defined function in javascript:


var supriseMe = function () {

alert("Boo!");

supriseMe = function () {
alert(" boooooooo!");
};
};
// using the self-defining function
supriseMe(); // Boo!
supriseMe(); // boooooboo!