JavaScript closure function quick to get started

Source: Internet
Author: User

What is a closure function? At the beginning of the closure of the study, we can all be more difficult to understand. From his official explanation, it is more conceptualized.


But we also start with the meaning of closures.

Closures are variables that have free and independent functions. In other words, a function defined in a closure can "remember" the environment it was created in.

After the official explanation, let's look at a simple example of counting.

var c = 0;function count () {C + +;} Count ();//1count ();//2


This example is implemented using global variables, but here's a problem where C is easily called by other means, which can change the stored value of C. Causes the expiration of this count count. How do you deal with this problem? We will think of the use of local variables, such as:

function count () {var c = 0;  function Add () {C + +; } add ();} Count ();//C = 1count ();//c = 1


Since this is created, the internal variable only exists when the Count function is executed, and the entire function is discarded after execution. It is impossible to achieve the ability to have memory. So how do we do that? Then we'll use closures to solve. I'm going to do it again: closure = function + Environment

function count () {var c = 0;   function Add () {C + +; } return add;} var ct = count (); CT (); c = 1ct (); c = 2

This is the time when we can complete the counting by this closure. CT is a closure function, the internal environment is the local variable C. What we have here is internal data, external to the operation. What other functions does the closure have in addition to this?


simulating private methods with closures

This is a bit like Java private methods or private variables, only allowed to operate on their own! If the external operation, you need to set a public method to operate.

var person =  (function () {    var _name =  "programmed Man";     var age = 20;    return {     add : function () {         age++;     },      jian:function () {         age--;      },     getage:function ()  {          return age;     },     getname: function () {         return _name;      },     setName: function  (name)  {          _name = name;                      }    }) ();p erson.add (); var age =  person.getage (); Console.log (age) Person.setname ("Programmed people public Number: Bianchengderen") Console.log (Person.getname ())


It should be easy to understand here! A bit of an object-oriented programming feeling. Of course, JavaScript now has the features of object-oriented programming. We will explain this later.

So far, we have from the count to the internal privatization of the example, to explain the closure, I hope you can easily understand the truth, of course, closures and other functional use, is more convenient.



This article is from the "Ophir Technology Research" blog, please be sure to keep this source http://ophir.blog.51cto.com/1741965/1723063

JavaScript closure function quick to get started

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.