Javascript closure function_javascript tips-js tutorial

Source: Internet
Author: User
This article mainly helps you easily learn about Javascript closure functions, starting from the meaning of closures, and learning Javascript closure functions from a simple perspective. If you are interested, you can refer What is a closure function?When we started learning the closure, it was very difficult for everyone to understand. From his official explanation, they were all relatively conceptual.

But we are still starting fromDescription of closureDeparture.
A closure is a function with independent variables. In other words, a function defined in a closure can "remember" the environment when it was created.
After the official explanation, let's look at a simple counting example.

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

In this example, global variables are used. However, the c variable is also easily called by other methods, which may change the storage value of c. the count is invalid. so how can we handle this problem well! We will think of using local variables for processing. For example:

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

This is because after the creation, the internal variable only exists when the count function is created and executed. After the execution, the entire function will be discarded. it is impossible to achieve the ability to have memories. how can we achieve this? Then we will use the closure to solve the problem. I will repeat it: closure = Function + Environment

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

In this case, we can use this closure to complete the counting capability. ct is a closure function, and the internal environment is the local variable c. what we achieve here is internal data and external operations. what other functions does the closure have besides this?

Simulate private methods with closures
This is a bit like a JAVA private method or private variable. You can only operate it on your own! If an external operation is performed, you need to set a public method for the operation.

Var person = (function () {var _ name = "programmer"; var age = 20; return {add: function () {age ++ ;}, jian: function () {age -- ;}, getAge: function () {return age ;}, getName: function () {return _ name ;}, setName: function (name) {_ name = name ;}}) (); person. add (); var age = person. getAge (); console. log (age) person. setName ("Programmer public account: bianchengderen") console. log (person. getName ())

It should be easy to understand here! It's a bit object-oriented. Of course, Javascript also has the characteristics of object-oriented programming. This will be explained later.
So far, we have explained the closure from counting to internal privatization. I hope you can simply understand the truth. Of course, it is more convenient to use the closure and other functions.

The above is all the content of this article, and I hope it will help you learn javascript programming.

Related Article

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.