本文介绍了这种模式f(a)(b)(c)叫什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数setStyle(x){
with(x.style){
padding ='10px'
marginBottom ='10px'
width ='100%'
}
返回setStyle
}

setStyle(帐户)(密码)(登录)

我经常使用上述模式,但是当我需要谈论它时,我不知道它叫什么。如何在交谈中引用这种方法?



更新:



有人将此问题视为重复,但我看不出为什么是重复的。其他问题是关于一些框架,海报试图了解代码。我不试图理解代码,我正在问一个已知模式的英文术语。尽管所讨论的概念是相同的,但问题是不同的。这就好像是说谁是Jasckson's Five中最年轻的人和谁的专辑是惊悚片都是同一个问题,只是因为它碰巧是同一个人。但这些是不同的问题,不一样。

解决方案

这种模式不是 功能链因为链接(=组合,管道化)函数通过将一个函数的结果作为参数传递给下一个函数来工作。例如,如果您有类似的内容:

  str = removeBadChars(str)
str = makeLowerCase(str)
str = convertToURL(str)

您可以更简洁地将其重写为

  str = chain(removeBadChars,makeLowerCase,convertToURL)(str)

(练习:写 chain())。



似乎没有名字,可能是因为它毫无用处。 Crockford先生术语 retursion
,但它似乎并没有被广泛使用。注意,这种模式本质上是反功能的,因为在函数式编程中我们更喜欢使用纯函数,而一个retursive函数只能做任何有用的副作用。



UPD:这个推理适用于只是盲目返回自己的函数,但是,在每次调用时, f()()()语法也可以用于返回其他东西的函数。在这种情况下,根函数接受一个隐藏的参数(累加器),用于携带状态。这种方法的一个很好的例子就是上面的 chain(),但是用适用的风格写成: chain(func1)(func2)(func3) :

  function chain(f,fun){fun = fun ||函数(x){return x};如果(typeof f!=function)返回fun(f);返回函数(x){返回链(x,函数(y){返回f(fun(y))})}} //示例:makeLowerCase = function(str){return str.toLowerCase()}; removeBadChars = function (str){return str.replace(/ \W / g,)}; makeURL = function(str){returnhttp://+ str}; funcs = chain(makeLowerCase)(removeBadChars)(makeURL ); x = funcs('!!! HELLO ???'); alert(x);  



在这种情况下,此语法用于实现部分应用程序,因为我们返回一个闭包,它再次调用根函数,并带有预定义的第二个参数。 b

function setStyle(x) {
    with (x.style) {
        padding = '10px'
        marginBottom = '10px'
        width = '100%'
    }
    return setStyle
}

setStyle(account)(password)(login)

I often use the above pattern, but when I need to talk about it, I have no idea what is it called? How to refer to this kind of approach in the conversation?

UPDATE:

Someone closed this question as duplicate, but I cannot see why is that a duplicate. Other question is about some framework, the poster is trying to understand the code. I do not try to understand the code, I am asking about an English term for a known pattern. Even though the discussed concept is the same, the question is different. It is like saying that "Who was the youngest in Jasckson's Five" and "Whose album is Thriller" both are the same question, only because it happens to be about the same person. But those are different questions, not the same.

解决方案

This pattern is not "function chaining", because chained (=composed, pipelined) functions work by passing result of one function as an argument to the next one. For example, if you have something like:

str = removeBadChars(str)
str = makeLowerCase(str)
str = convertToURL(str)

you can rewrite this more concisely as

str = chain(removeBadChars, makeLowerCase, convertToURL)(str)

(Exercise: write chain()).

Your pattern doesn't appear to have a name, probably because it's fairly useless. Mr. Crockford coined the term retursion,but it doesn't seem to be used widely.

Note that this pattern is essentially anti-functional, because in functional programming we prefer to work with pure functions, while a "retursive" function can only do anything useful as a side effect.

UPD: this reasoning applies to functions that just blindly return itself, like in your example, however, the f()()() syntax can also be used with functions that return something else on each invocation. In this case, the "root" function accepts a hidden parameter ("accumulator") used to carry state around. A good example of such approach would be chain() like above, but written in the applicative style: chain(func1)(func2)(func3):

function chain(f, fun) {

    fun = fun || function(x) { return x };

    if(typeof f != "function")
        return fun(f);

    return function(x) {
        return chain(x, function(y) { return f(fun(y)) })
    }
}

// Example:

makeLowerCase  = function(str) { return str.toLowerCase() };
removeBadChars = function(str) { return str.replace(/\W/g, "") };
makeURL        = function(str) { return "http://" + str };

funcs = chain(makeLowerCase)(removeBadChars)(makeURL);
x = funcs('!!! HELLO ???');
alert(x);

In this case, this syntax is used to implement "partial application", because we return a closure which calls the root function yet again, with a predefined second argument.

这篇关于这种模式f(a)(b)(c)叫什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:03