Simple Closure Test

by nickadeemus2002

HTML

<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

JavaScript

/*
myVar ='outer';
var foo = (function () {
    var myVar = "local";
    return function () {
        return window.myVar;
    };
}());
var output = foo();
console.log(output);
*/

/*
(function() {
   var a = b = 5; //only a is locaal, no var for b
})();
console.log(b);
*/

/*
String.prototype.repeatify = function(repeatNo){
    var str='';
    if(repeatNo > 0 && repeatNo !== undefined && !isNaN(repeatNo)){                         
        for(var i=0; i< repeatNo; i++){
           str += this;
        }    
    }else{
        str='please define a valid number for repeat string.';
    }
    return str;
};
console.log('hello'.repeatify(20));
*/

/*
function test() {    
   console.log(a);  //undefined
   console.log(foo()); //foo exists as it's hoisted
   var a = 1; //never used
   function foo() {
      return 2;
   }
}
test();
*/

/*
var fullname = 'John Doe';
var obj = {
   fullname: 'Colin Ihrig',
   prop: {
      fullname: 'Aurelio De Rosa',
      getFullname: function() {
         return this.fullname;
      }
   }
}; 
console.log(obj.prop.getFullname()); //'Aurelio De Rosa'
var test = obj.prop.getFullname;
console.log( test() );  //'John Doe
console.log( test.call(obj.prop) );  //'Aurelio De Rosa'
*/

var foo = [];
foo.push(1);
foo.push(2);

//function add(a,b){
//console.log('a');
//console.log(a);
//console.log('b');
//console.log(b);
//console.log( (a + b) );
//}




function add(a,b) {
    var x=null,
        y=null;
    
    //closure to hanlde
    //single param case
    function stored(y){   
        if(x == null){
            return ( x + y );
        }else{
            console.log('using closure for add method');
            console.log(x+y);        
        }        
    }
    
    
    if(arguments.length == 2){           
        console.log('you entered two parameters for add method');
        console.log((a+b));
    }else{
        if( x == null){
           x = a;
           return stored;
        }
    }
}
add(2, 5);   // 7
add(2)(5);   //...