Atul Jain
What are Javascript closures? Explain its Uses?
By Atul Jain in JavaScript on Aug 13 2012
  • Abhay Shanker
    Nov, 2014 12

    A closure is a pairing of a function along with its referencing environment such that identifiers within the function may refer to variables declared within the referencing environment.It's an inner function that has access to the outer (enclosing) function's variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function's variables, and it has access to the global variables.function showName (firstName, lastName) {? ?var nameIntro = "Your name is ";// this inner function has access to the outer function's variables, including the parameter? ?function makeFullName () {? ?return nameIntro + firstName + " " + lastName;? } ? ?return makeFullName ();? }? ? showName ("Michael", "Jackson"); Closures can be used to accomplish many things. They are very useful for things like configuring callback functions with parameters.Closures are useful when used in conjunction with the setTimeout() and setInterval() functions.

    • 0
  • Jaganathan Bantheswaran
    Nov, 2014 12

    A closure is an inner function that has access to the outer (enclosing) function's variables as well as its own.You can use closures to implement encapsulation something like this,a = (function () {var privatefunction = function () {alert('hello');}return {publicfunction : function () {privatefunction();}} })();

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS