Curialized functions: Javascript; colialized javascript
Curialization functions in Javascript
Definition
According to the definition, keri is to split a function that receives "multiple" parameters into one or more functions that receive "single" parameters. The definition looks abstract. The following is an example:
Code
12345 |
function concat(str1,str2){return str1 + str2;}concat("forever","px") // "foreverpx" |
It is easy to understand that the code above defines a function to connect strings and returns strings that connect two input parameters. Now let's look at another definition method:
12345678 |
function concatCurry(str1){return function(str2){return str1 + str2;}}var concatOne = concatCurry("forever");concatOne("px"); //foreverpx |
In the code above, we converted the function that passed two parameters at the beginning into one (or two) function that only received one parameter.
Let's create a universal function that accepts multiple parameters:
123456789 |
function curry(fn) { var args = [].slice.call(arguments, 1); return function() { return fn.apply(null, args.concat([].slice.call(arguments, 0))); }}var concatOne = curry(concat,"forever");concatOne("px"); //foreverpx |
The Keri process uses the closure so that the returned function can obtain the first input parameter.
First, the second input parameter is saved to The args variable, and then a function is returned. In the returned function, the first input parameter and the parameters passed in after colialization are spliced into an array, then use the apply method to call the function (fn) before colialization, so that the result of colialization is achieved.
If a function with three parameters wants to be colized, the following is true:
12345678 |
function concat(s1,s2,s3){return s1+s2+s3;}var cur1 = curry(concat,"forever");var cur2 = curry(cur1,"px");cur2("-me"); //foreverpx-me |
Author: foreverpx
Link to the original article: The Javascript of colized Functions
Functions in javascript
Using functions can increase our webpage control capabilities. For example, you can check the input content in the text box. When I click a button or fill in the content in the text box, operate other controls. For your own use, you still need to invest some time to think about it.
For example, check the input IP address. If an error occurs, a warning box is displayed:
This is a function:
<Script language = "javascript">
Function verifyip ()
{
Var reg =/^ (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) (\. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) {3} $ /;
Var ip1 = document. form1.ip1. value;
If (ip1 = "")
{Alert ("IP cannot be blank"); return false ;}
Else if (! (Ip1.match (reg )))
{Alert ("IP must be 0.0.0.0-255.255.255.255"); return false ;}
Else return true;
}
The following is the form part of the Web page. We call the verification function when submitting:
<Form id = "form1" name = "form1" method = "post" action = "">
<Input name = "ip1" type = "text" id = "ip1" size = "15">
<Input style = "position: absolute; top: pixel PX; left: pixel PX; width: 80px; height: 30px; color: green; "type =" submit "name =" submit1 "value =" submit "onclick =" return verifyip () ">
</Form>
This is just a simple example. If you are interested, you can read a book on javascript. In fact, everything on the market is similar.
What is the javascript constructor?
Javascript itself does not have the concept of classes, but only functions. The javascript class is actually a javascript function. In this special function, it can contain variables and references of other javascript Functions. The special function itself is the so-called class constructor of javascript.