简体   繁体   English

像某些函数一样的JavaScript函数调用(“ Hi”)(“ Hello”)(“ How”)

[英]Javascript function call like somefunction(“Hi”)(“Hello”)(“How”)

Please have a look at this code. 请看一下这段代码。 I need to show a alert message "mikä on elämän tarkoitus?" 我需要显示一条警报消息“elämäntarkoitus上的mikä?” using this code 使用此代码

window["mikä"]("on")("elämän")("tarkoitus")("?"); 

I need to write a function or piece of code that will show that alert message when I will execute that code. 我需要编写一个函数或一段代码,以在执行该代码时显示该警报消息。

I have written a function like this: 我写了这样的函数:

window["mikä"] = function(str){
alert(str);
}

which works when I call window"mikä" but if I add more like below in console I see a type error. 当我将窗口称为“mikä”时,它可以工作,但是如果在控制台中添加如下所示的内容,则会看到类型错误。

 window["mikä"]("on")("Hello")("How"); 

My question is would it be valid way to call like below as there is multiple function signs? 我的问题是,由于存在多个功能标志,因此像下面这样调用是一种有效的方法吗?

window["mikä"]("on")("elämän")("tarkoitus")("?") 

To achieve the functionality you are looking for one way is to write a function which returns a function which returns a function as the others mentioned. 要实现该功能,您正在寻找一种方法,即编写一个函数,该函数返回一个函数,该函数返回另一个函数。 That works fine if the number of functions is known before hand. 如果事先知道函数的数量,那会很好。 Another way is to use a functional programming technique called currying , which is 另一种方法是使用称为currying的功能编程技术,即

the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument (partial application). 将具有多个参数(或参数的元组)的函数的评估转换为评估一系列函数(每个函数具有单个参数)的技术(部分应用程序)。

You can write your own curry function like this: 您可以编写自己的咖喱函数,如下所示:

function curry(func, args_) {
    var self = this;

    self.args = args_ || [];

    return function() {
        var extended_args = [].concat(self.args).concat(Array.slice(arguments));

        if(extended_args.length >= func.length)
            return func.apply(this, extended_args);

        return new curry(func, extended_args);
    };
}
var funcName = "mikä";
window[funcName] = curry(functionstr1, str2, str3, str4) {
    alert(funcName + ' ' + str1 + ' ' + str2 + ' ' + str3 + str4);    
});
window["mikä"]("on")("elämän")("tarkoitus")("?");

Here are some resources which can help you if you are interested in learning more about currying / functional programming in JS. 如果您有兴趣学习有关JS中的currying /函数式编程的更多信息,那么这里有一些资源可以为您提供帮助。

http://kukuruku.co/hub/javascript/an-interesting-task-for-an-interview-currying-and-partial-applicationof-a-function http://tech.pro/tutorial/2011/functional-javascript-part-4-function-currying Reginald Braithwaite's talk in NDC Oslo http://kukuruku.co/hub/javascript/an-interesting-task-for-an-interview-currying-and-partial-application-a-function http://tech.pro/tutorial/2011/functional-javascript NDC奥斯陆的 Regal-part-4-function-currying 雷金纳德· 布雷思韦特 的演讲

You want the return value to be a function as well, so that additional calls on the return value will call the same function. 您还希望返回值也是一个函数,以便对返回值的其他调用将调用相同的函数。 Just add this 只需添加此

window["mikä"] = function(str){
  alert(str);
  return window["mikä"];
}

EDIT: Misread your question, this will make multiple alert messages. 编辑:误读了您的问题,这将使多个警报消息。 Sorry. 抱歉。

You probably want to nest the function calls 您可能想嵌套函数调用

 window["mikä"] = function(s1){ return function(s2) { return function(s3) { alert(s1 + ' ' + s2 + ' ' + s3); } } } window["mikä"]("on")("elämän")("tarkoitus")("?"); 

As for getting the function name inside the function, there's really no good way to do that, and it should be avoided. 至于在函数内部获取函数名称,实际上并没有很好的方法,应避免这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM