JavaScript closure closure

Source: Internet
Author: User
Tags closure


* Implement function Makeclosures, after the call satisfies the following conditions:
1. Returns a function array result with the same length as arr
2. Run the first function in result, i.e. result[i] (), the result is the same as FN (Arr[i])

Input

[1, 2, 3], function (x) {return x * x;}
function Makeclosures (arr, fn) {    var result = [];        for (var i = 0; i < arr.length; i++) {        (function (i) {            Result.push (function () {                return fn (Arr[i]);            });        }) (i);    }        return result;} var arr = [1, 2, 3];var fn = function (x) {return x * x;} var result = Makeclosures (arr, FN); Console.log (result);  [F,f, F, F]console.log (Result[1] ());   4console.log (FN (arr[1]));   4

  

The known function fn execution requires 3 parameters. Implement the partial function, which satisfies the following conditions:
1. Returns a function result, which takes a parameter
2. Execution result (STR3), the returned result is consistent with FN (STR1, str2, STR3)

Input:var sayit = function (greeting, name, punctuation) {return greeting + ', ' + name + (Punctuation | | ‘!‘);  }; Partial (sayit, ' Hello ', ' Ellie ') ('!!! ');

Output:hello, Ellie!!!

function partial (FN, str1, str2) {return function (STR3) {return fn (str1, str2, STR3);}} var sayit = function (greeting, name, punctuation) {return greeting + ', ' + name + (Punctuation | | ‘!‘);}; Console.log (Partial (sayit, ' Hello ', ' Ellie ') ('!!! '));

  

  

JavaScript closure closure

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.