Skip to main content

JavaScript Closures

JavaScript Closures



  • JavaScript variables can belong to the local or global scope.
  • Global variables can be made local (private) with closures.
  • a closure is one way of supporting first class functions, it is an expression that can reference variables within its scope (when it was first declared), be assigned to a variable, be passed as an argument to a function, or be returned as a function result. Or
  • a closure is a stack frame which is allocated when a function starts its execution, and not freed after the function returns.

example -


var add = (function () {
    var counter = 0;
    return function () {return counter += 1;}
})();

add();
add();
add();

// the counter is now 3

What I learnt in Lab class..


Create a separate function using JavaScript closure which accepts the tax percentage and
returns a function which accepts the amount and returns the amount after adding tax
percentage.





// closure is used to hide the inner implementation
function tax (taxPercentage){
    var tPer = taxPercentage; // assinging the parameter to a variable
    //outer function implementation
    return function netTax (amount) //it is like a function inside a function
    {
        var nTax = amount+tPer; // net tax is calculated adding the parameters
        return nTax;
    };

}

var f1 = tax(20); //calling the tax  method
console.log(f1(30));





Comments

Popular posts from this blog

MongoDB Up & Running

MongoDB Up & Running What is mongoDB ?             Mongo DB has rapidly grown to become a popular database for web applications and is a perfect fit for Node.JS applications, letting you write Javascript for the client, backend and database layer. Its schemaless nature is a better match to our constantly evolving data structures in web applications, and the integrated support for location queries is a bonus that’s hard to ignore. Throw in Replica Sets for scaling, and we’re looking at really nice platform to grow your storage needs now and in the future. MongoDB is an   open source   database that uses a document-oriented data model.MongoDB is one of several   database   types to arise in the mid-2000s under the   NoSQL   banner. Instead of using   tables   and   rows   as in   relational databases, MongoDB is built on an architecture of collections and documents. Do...

What is a java HashMap and How it works ?

What is a java HashMap and How it works ? What is a HashMap ?             The HashMap in Java is one of the most popular Collection class among Java programmers. The HashMap is a data structure, based on hashing, which allows you to store an object as key value pair, an advantage of using HashMap is that you can retrieve object on constant time i.e. O(1) if you know the key. The HashMap class implements Map interface and supports Generics from Java 1.5 release, which makes it type safe. There are a couple of more Collections, which provides similar functionalities like HashMap, which can also be used to store key value pair. Hashtable is one of them, but Hashtable is synchronized and performs poorly in a single threaded environment. one relatively new is ConcurrentHashMap, which provides better performance than Hashtable in a concurrent environment and should be preferred. How HashMap works ?     ...

Ajax and API

API  - App lic ation Pro gra m Inter face   It is a  set of functions and procedures that allow the creation of applications which access the features or data of an operating system, application, or other service. what i understood.. It is like a middle ware between user and the database ( can made from java/PHP/java script) . Not giving the full access to the user Hiding the real data ( like a SQL view or can be real data too) in web services we use " ajax " to access the  database through API ( not always ) user uses --> ajax --> to access API -->database AJAX  - Asynchronous JavaScript and XML  In a nutshell, it is the use of the XMLHttpRequest object to communicate with server-side scripts. It can send as well as receive information in a variety of formats, including JSON, XML, HTML, and even text files AJAX is a developer's dream, because you can: Update a web page without reloading the page Request data from a se...