在Java语言中使用闭包的好例子 [英] Good examples for using a Closure in Javascript

查看:45
本文介绍了在Java语言中使用闭包的好例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我最近了解了Java语言中的闭包.

Well, I recently learned about closures in Javascript.

虽然我觉得它的概念确实很棒,但是我自己还没有找到适合他们的应用程序.

While i find it's concept truly amazing, i have yet to find a good application for them, myself.

在所有博客文章中,我找到了所有的教程,我对它们是什么以及如何使用它们进行了很好的解释.

In all the blog posts, all the tuturials i found, i got a good explanation of what they are and how to work with them.

我在任何地方都找不到的例子让我想到:哇!你可以用闭包来做到这一点?太棒了!!!我发现的所有例子都是纯学术性的.

What i can't find anywhere are examples that make me think: "Wow! you can do THIS with closures? Awesome!!!". All the examples i find are purely academic like this one.

function say667() {
  // Local variable that ends up within closure
  var num = 666;
  var sayAlert = function() { alert(num); }
  num++;
  return sayAlert;
}

var sayNumber = say667();
alert(sayNumber());

因此,我想知道你们中的任何人是否可以通过这些特殊功能分享一些令人振奋的经验.

So, i was wondering if any of you can share some mind-blowing experiences with these special kind of functions.

我知道这是一个悬而未决的问题,但我会将答案归功于最让我哇的人.

I know that this is sort of an open question, but i will attribute the answer to whoever makes me WOW the most.

谢谢

推荐答案

咖喱变量

由于闭包"只是说一个函数始终保留其原始变量范围的一种方式,因此有很多方法可以利用它.

Currying variables

Since "closure" is just a way of saying that a function always retains its original variable scope, there are many ways you can take advantage of that.

咖喱似乎是人们喜欢的东西.

Currying seems to be something that people like.

在这里,我创建了一个函数 curry ,该函数将返回一个函数,该函数将用于生成使用原始咖喱值的新函数.

Here I've created a function curry that will return a function that will be used to generate new functions that work with the original curried value.

function curry() {
    var args = Array.prototype.slice.call(arguments);
    return function(fn) {
        return function() {
            var args2 = Array.prototype.slice.call(arguments);
            return fn.apply(this,args.concat(args2));
        };
    };
}


创建可咖喱字符串的函数

因此,如果我想创建使用字符串的函数,则可以使用该字符串...


Creating a function that curries a string

So if I want to create functions to work with a string, I could curry the string...

var workWithName = curry("Bubba");

...并使用返回的函数创建以各种方式与给定字符串一起使用的新函数.

...and use the function returned to create new functions that work with the given string in various ways.

在这里我创建一个 talkToName 函数,该函数将根据传递的参数将名称合并到各种句子中.

Here I create a talkToName function that will incorporate the name into various sentences based on the arguments passed...

var talkToName = workWithName(function(curried_str, before, after) {
    return before + curried_str + after;
});

所以现在我有一个 talkToName 函数,该函数接受2个包装了咖喱字符串的字符串.

So now I have a talkToName function that accepts 2 strings that wrap the curried string.

talkToName("Hello there ", ". How are you?"); // "Hello there Bubba. How are you?"
talkToName("", " is really super awesome.");  // "Bubba is really super awesome."

通知,我将两个参数传递给 talkToName 函数,但是赋予 workWithName 的函数接受3个参数.

Notice that I pass two arguments to the talkToName function, but the function given to workWithName accepts 3 arguments.

第一个参数由我们从 workWithName()创建的函数传递,我们给 talkToName 的两个参数将添加到原始的咖喱参数之后.

The first argument is passed by the function we created from workWithName(), and the two arguments we give to talkToName are added after the original curried argument.

在这里,我使用原始的 workWithName 函数创建了一个完全不同的函数,该函数将使用"Bubba"字符串,并返回一个字符串,该字符串的字母以给定值递增...

Here I create an entirely different function using the original workWithName function that will take the "Bubba" string, and return a string with the letters incremented by the given value...

var incrementName = workWithName(function(curried_str, n) {
    var ret = '';
    for(var i = 0; i < curried_str.length; i++) {
        ret += String.fromCharCode(curried_str[i].charCodeAt() + n);
    }
    return ret;
});

所以我给新的 incrementName 函数传递了一个数字,它使名称中的字母递增,并返回新的字符串...

So I pass my new incrementName function a number, and it increments the letters in the name, and returns the new string...

incrementName(3);  // "Exeed"
incrementName(8);  // "J}jji"
incrementName(0);  // "Bubba"

因此您可以看到我们给了 curry()一个值,并且它给了我们一个函数,该函数可用于创建使用原始值的新函数.

So you can see that we gave curry() a value, and it gave us back a function that can be used to create new functions that work with the original value.

再次通知,我将一个参数传递给 incrementName 函数,但是赋予 workWithName 的函数接受2个参数.第一个论点是咖喱.

Notice again that I pass one argument to the incrementName function, but the function given to workWithName accepts 2 arguments. The first argument is curried.

下面是一个示例,该示例创建一个函数生成器,该函数生成器使用数字 3 5 .

Here's an example that creates a function generator that works with the numbers 3 and 5.

var workWith3And5 = curry(3, 5);


创建函数,以处理咖喱号码

因此,使用 workWith3And5 函数,我们创建了一个新函数,该函数将接受数字参数,并返回具有给定数字的咖喱数之和的数组...


Create functions that do various things with the curried numbers

So using the workWith3And5 function, we make a new function that will accept a number argument, and return an Array of the sums of the curried numbers with the given number...

var addNTo3And5 = workWith3And5(function(x, y, n) {
    return [3 + n, 5 + n];
});

addNTo3And5( 8 );  // [11, 13];
addNTo3And5( -4 ); // [-1, 1];


另一个使用相同的 workWith3And5 函数处理数字 3 5 的函数,该函数创建一个3 x 5的数组数组,其中嵌套数组具有一些内容...


And another one using the same workWith3And5 function that curries the numbers 3 and 5 that creates a 3 x 5 Array of Arrays, where the nested Array is given some content...

var create3By5GridWithData = workWith3And5(function(x, y, data) {
    var ret = []
    for(var i = 0; i < x; i++) {
        ret[i] = [];
        for(var j = 0; j < y; j++) {
           ret[i][j] = data;
        }
    }
    return ret;
});

create3By5GridWithData( 'content' ); // [Array[5], Array[5], Array[5]]

这篇关于在Java语言中使用闭包的好例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆