closures and curialization are common and advanced techniques in Javascript, all functional programming languages support these two concepts. Therefore, we want to give full play to the functional programming features in JavaScript, we need to have a deep understanding of these two concepts. Closure is actually an indispensable foundation for colitization.
1. Concepts of colialization
in computer science, curialization converts a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the initial function, and return the technology of the new function that accepts the remaining parameters and returns the result. This technique was named by Christopher Strachey as the logologist Haskell curry, though it was invented by Moses schnfinkel and gottlob Frege. Intuitively, curialization claims that "if you fix some parameters, you will get a function that accepts the remaining Parameters ". Therefore, if y = 2 is fixed for the Yx function with two variables, a variable's function 2x is obtained.
ke Lihua transfers some parameters of the function in advance to obtain a simple function. But the pre-passed parameters are stored in the closure, so there are some special features. For example:
VaRAdder =Function(Num ){Return Function(Y ){ReturnNum +Y ;}}VaRINC = adder (1);VaRDec = adder (-1)
The INC/DEC variables here are actually two new functions that can be called through brackets. For example, the usage in the following example:
//INC and Dec are now two new functions used to pass in the parameter value (+/-) 1Print (Inc (99 ));//100Print (Dec (101 ));//100Print (adder( 100) (2 ));//102Print (adder (2) (100 ));//102
Ii. Applications of Keri
Based on the features of Keri, we can write more interestingCodeFor example, in front-end development, we often encounter this situation. When a request is returned from the server, we need to update some specific page elements, that is, the concept of partial refresh. Partial refresh is very simple, but the code is easy to write. If you use Keri, You can beautify your code to make it easier to maintain. Let's look at an example:
// Update returns a function that can set the content of the Web element whose ID attribute is item. Function Update (item ){ Return Function (Text) {$ ( "Div #" + Itempai.html (text );}} // Ajax request. when the request is successful, the callback parameter is called. Function Refresh (URL, callback ){ VaR Params = {Type: "Echo" , Data: ""}; $. Ajax ({type: "Post" , URL: URL, cache: False , Async: True , Datatype: "JSON" , Data: Params, // Called when the asynchronous request is successful Success: Function (Data, status) {callback (data );}, // Called when an error occurs in the request Error: Function (ERR) {alert ( "Error:" + Err) ;}}) ;}refresh ( "Action. do? Target = News ", update (" newspanel" ); Refresh ( "Action. do? Target = articles ", update (" articlepanel" ); Refresh ( "Action. do? Target = pictures ", update (" picturepanel" ). The update function is an instance of colized. It returns a function, that is, update ( "Newspanel") = Function (Text) {$ ( "Div # newspanel" Pai.html (text );}
Since the return value of update ("newspanel") is a function and the required parameter is a string, In the Ajax call of refresh, when success, the callback is passed into the data information returned by the server to refresh the newspanel panel.ArticleThis method is used to refresh the Panel articlepanel and picturepanel. In this way, the code readability and maintainability are improved.
From: http://www.yiiyaa.net/1314
Other related articles: http://book.51cto.com/art/201211/367101.htm
Http://www.zhangxinxu.com/wordpress/2013/02/js-currying/