Created
September 19, 2010 06:45
-
-
Save sriprasanna/586490 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple example to show the power of closure when exploited w.r.t scope | |
function power_of (power) { | |
return function (number) { | |
return Math.pow(number, power); | |
}; | |
} | |
var power_of_3 = power_of(3), | |
power_of_4 = power_of(4); | |
console.log("2^3", power_of_3(2)); // 8 | |
console.log("2^4", power_of_4(2)); // 16 | |
// Example to show how variable hoisting works in JS | |
var x = 1; | |
(function () { | |
console.log( "Global variable x is accessed in local scope with same value = ", x ); // 1 | |
})(); | |
var x = 1; | |
(function () { | |
x = 2; | |
console.log("Because of variable hoisting x is intialized in local scope with value = ", x); // 2 | |
return; | |
function x () {}; | |
})(); | |
console.log("x in global scope remains untouched with value = ", x); // still remains 1 | |
var x = 1; | |
(function () { | |
var x = x || 2; | |
console.log("Because of variable hoisting x is intialized in local scope with value = ", x); // 2 | |
})(); | |
console.log("x in global scope remains untouched with value = ", x); // still remains 1 | |
var x = 1; | |
(function () { | |
console.log("Because of variable hoisting x is intialized without any values in local scope = ", x); // undefined | |
var x; | |
})(); | |
console.log("x in global scope remains untouched with value = ", x); // still remains 1 | |
var x = 1; | |
(function () { | |
x = x || 2; | |
var x; | |
console.log("Because of variable hoisting x is intialized in local scope with value = ", x); // 2 | |
})(); | |
console.log("x in global scope remains untouched with value = ", x); // still remains 1 | |
var x = 1; | |
(function () { | |
x = x || 2; | |
console.log("Since x is not initialized in local scope it picks the one in global scope with value = ", x); // 1 | |
})(); | |
console.log("x in global scope remains untouched with value = ", x); // still remains 1 | |
var x = 1; | |
(function(){ | |
console.log("Because of variable hoisting x is intialized without any values in local scope = ", x); // undefined | |
return; | |
var x; | |
})(); | |
console.log("x in global scope remains untouched with value = ", x); // still remains 1 | |
// Private variables can be set in JS using closures | |
function Dog () { | |
var no_of_times_barked = 0; | |
this.bark = function () { | |
console.log("Woof"); | |
no_of_times_barked++; | |
}; | |
this.getNoOfTimesBarked = function () { | |
return no_of_times_barked; | |
}; | |
} | |
var dog = new Dog(); | |
console.log("No of times barked = ", dog.getNoOfTimesBarked()); | |
dog.bark(); | |
console.log("No of times barked = ", dog.getNoOfTimesBarked()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment