JavaScript closure and function of curry analysis _ basic knowledge

Source: Internet
Author: User

Closures and Corrie are all frequently used and advanced techniques for JavaScript, and all functional programming languages support both concepts, so we want to give full play to the functional programming features of JavaScript, and we need to understand both concepts in depth. Closures are in fact the essential foundation of the Gerty.

One, the concept of Gerty

In computer science, Gerty transforms a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the original function), and returns a technique that accepts the remaining parameters and returns a new function of the result. This technique was named by Christopher Strachey, Haskell Curry of the logic family, although it was invented by Moses Schnfinkel and Gottlob Frege. Intuitively, Corrie claims that "if you fix certain parameters, you will get a function that takes the rest of the parameters". So for function yx with two variables, if y = 2 is fixed, then the function 2x with a variable is obtained.

Gerty is to pass some parameters of a function in advance and get a simple function. But the parameters that are passed in are stored in the closure, so there are some peculiar features. Like what:

var adder = function (num) {return
  function (y) {return
     num + y;
  }
}
var inc = Adder (1);
var dec = adder (-1)

Here the Inc/dec two variables are in fact two new functions, which can be called by parentheses, such as in the following example:

Inc., Dec is now two new functions that will pass in the parameter values passed in (+ +) 1
Print (inc),//100 print (Dec ()),
//100
print (Adder (100) ( 2));//102
print (Adder (2) (100));//102

Second, the application of curry

According to the characteristics of the curry, we can write more interesting code, such as in front-end development often encounter this situation, when the request from the server side back, we need to update some specific page elements, that is, the concept of local refresh. Using local refreshes is easy, but the code can easily be written as mess. And if you use Gerty, you can largely beautify our code to make it easier to maintain. Let's take a look at an example:

//update returns a function that sets the Content function update (item) {return function of the Web element with an ID property of item (text
  ) {$ ("div#" +item). html (text);
  }//ajax request, when success is called parameter callback function refresh (URL, callback) {var params = {type: "echo", Data: ""};
     $.ajax ({type: "Post", Url:url, Cache:false, Async:true, DataType: "JSON", Data:params,
     The Success:function (data, status) {callback (data) is invoked when the asynchronous request succeeds;
     /////Call Error:function (ERR) {alert ("Error:" +err) when an error occurs on the request;
}
  });
Refresh ("Action.do?target=news", Update ("Newspanel"));
Refresh ("Action.do?target=articles", Update ("Articlepanel"));
Refresh ("Action.do?target=pictures", Update ("Picturepanel")); 
Where the update function is an instance of Gerty, it returns a function, namely: Update ("newspanel") = function (text) {$ ("Div#newspanel"). html (text); 

Because the return value of update ("Newspanel") is a function, the parameter required is a string, so in the Ajax invocation of refresh, when success, it gives callback incoming server-side returned data information. In order to achieve Newspanel panel refresh, the other article panel articlepanel, picture panel Picturepanel Refresh All take this way, so that the readability of the code, maintainability have been improved.

The above is the entire contents of this article, learn more JavaScript syntax, you can see: "JavaScript Reference tutorial", "JavaScript Code style guide," And I hope that we support the cloud-Habitat community.

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.