In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what is the use of JavaScript function Coriarization for you. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Corialization of function
A topic closely related to function binding is function function currying, which is used to create functions that have one or more parameters set. The basic method of function Corialization is the same as function binding: a closure is used to return a function. The difference between the two is that when the function is called, the returned function also needs to set some parameters passed in.
Function add (num1, num2) {return num1 + num2;} function curriedAdd (num2) {return add (5, num2);} console.log (add (2,3)); / / 5console.log (curriedAdd (3)); / / 8
This code defines two functions: add () and curriedAdd (). The latter is essentially a version of add () with the first argument of 5 in any case. Although technically curriedAdd () is not a Corialized function, it demonstrates its concept very well.
A Corialized function is usually created dynamically by calling another function and passing it the function and necessary parameters that you want to Corify.
Here is a common way to create a Corey function:
Function curry (fn) {var args = Array.prototype.slice.call (arguments, 1); return function () {var innerArgs = Array.prototype.slice.call (arguments), finalArgs = args.concat (innerArgs); return fn.apply (null, finalArgs);};}
The main job of the curry () function is to sort the parameters of the returned function. The first argument to curry () is the function to be Corified, and the other arguments are the values to be passed in.
To get all the parameters after the first parameter, the slice () method is called on the arguments object, passing in parameter 1 indicating that the returned array contains all the parameters starting with the second parameter. The args array then contains parameters from the external function. In the inner function, the innerArgs array is created to hold all the parameters passed in (again using slice ()).
Once you have an array of parameters from external and internal functions, you can use the concat () method to combine them into finalArgs, and then use apply () to pass the results to the function. Note that this function does not take into account the execution environment, so the first argument when calling apply () is null. The curry () function can be applied in the following ways.
Function add (num1, num2) {return num1 + num2;} var curriedAdd = curry (add, 5); alert (curriedAdd (3)); / / 8
In this example, the first Corey version of add () with a parameter bound to 5 is created. When cuurriedAdd () is called and 3 is passed in, 3 becomes the second parameter of add (), while the first parameter is still 5, and the final result is and 8. You can also give all the function parameters as in the following example:
Function add (num1, num2) {return num1 + num2;} var curriedAdd2 = curry (add, 5,12); alert (curriedAdd2 ()); / / 17
Here, both parameters of the add () function are provided, so there is no need to pass them later, and the function Corrigenization is often included as part of the function binding to construct a more complex bind () function.
Function bind (fn, context) {var args = Array.prototype.slice.call (arguments, 2); return function () {var innerArgs = Array.prototype.slice.call (arguments), finalArgs = args.concat (innerArgs); return fn.apply (context, finalArgs);};}
The main change to the curry () function is the number of parameters passed in and how it affects the result of the code. Curry () accepts only a function to be wrapped as an argument, while bind () accepts both a function and an object object.
This means that the argument to the bound function starts with the third instead of the second, which changes the first call to slice (). Another change is to pass the object object to apply () on the penultimate line. When bind () is used, it returns functions bound to a given environment, and it is possible that some of its function parameters have been set.
This is useful when you want to pass parameters to the event handler in addition to the event object.
Var handler = {message: "Event handled", handleClick: function (name, event) {alert (this.message + ":" + name + ":" + event.type);}}; var btn = document.getElementById ("my-btn"); EventUtil.addHandler (btn, "click", bind (handler.handleClick, handler, "my-btn"))
The handler.handleClick () method accepts two parameters: the name of the element to be processed and the event object. The name passed to the bind () function as the third argument is passed to handler.handleClick (), and handler.handleClick () also receives the event object.
The bind () method of ECMAScript5 also implements the function Corey, as long as another parameter is passed after the value of this.
Var handler = {message: "Event handled", handleClick: function (name, event) {alert (this.message + ":" + name + ":" + event.type);}}; var btn = document.getElementById ("my-btn"); EventUtil.addHandler (btn, "click", handler.handleClick.bind (handler, "my-btn"))
The Corialization function and binding function in javaScript provide powerful dynamic function creation function. Whether to use bind () or curry () depends on whether an object object response is required. Both of them can be used to create complex algorithms and functions, but neither should be abused, because each function carries additional overhead.
This is the end of this article on "what is the use of JavaScript function Corialization". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.