简体   繁体   English

将动作分配给变量

[英]Assigning actions to a variable

In answering Aaron's recent question , I'd like to do something like the following:在回答Aaron 最近的问题时,我想做以下事情:

rule first_rule {
    select when pageview "exampley.com/\?name=(.*)" setting (username)
    pre {
        isjoe = username eq "joe";
        myaction = defaction() {
            thisaction = isjoe => notify("Hello, World", "Hi there, Joe!") | noop();
            thisaction();
        };
    }
    {
        notify("Will it work?", "Methinks you are #{username}");
        myaction();
    }
}

However, the defaction never seems to work.然而,反抗似乎从来没有奏效。 It doesn't like that I'm trying to assign an action to a variable and then return that variable.它不喜欢我试图将一个动作分配给一个变量然后返回该变量。

What am I doing wrong?我究竟做错了什么?

You are really close.你真的很亲近。

You can't call an action till the end of the defaction.在反抗结束之前,您不能采取行动。 You need to create a defaction that delays execution till the right time.您需要创建一个延迟执行,直到正确的时间。

Change:改变:

thisaction = isjoe => notify("Hello, World", "Hi there, Joe!") | noop();

to

thisaction = isjoe => defaction(){notify("Hello, World", "Hi there, Joe!");} | noop;

Note the added defaction and I removed the parens from noop .请注意添加的缺陷,我从noop中删除了括号。

This concept is is similar to javascript closures.这个概念类似于 javascript 闭包。

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

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