从WebhookScript全局变量中检索函数

huangapple go评论55阅读模式
英文:

Retrieving A Function From A WebhookScript Global Variable

问题

在WebhookScript中,我可以将一个函数存储在变量中,如下所示:

sub = function(a, b) {
    return a - b
}

我想将一个函数存储在全局变量中,以便在多个自定义操作中使用它。但是,如果我将上面的函数保存为 $sub$,然后执行以下代码:

sub2 = var('$sub$')
subX = sub(1, 2)

会导致错误:

尝试调用非函数的 'string' @ 行...

以及

function subX(a, b){
    var('$sub$')
}

sub 只包含 return a - b 时也无法工作。

显然,我需要将字符串转换为函数,但我不确定是否可能。

我知道这是一种有点晦涩的语言,但如果有人知道如何在类似的语言(如JavaScript和PHP)中完成这个操作,我愿意尝试任何猜测...

英文:

In WebhookScript, I can store a function in a variable with:

sub = function(a, b) {
    return a - b
}

I'd like to store a function in a Global Variable so that I can use it in multiple Custom Actions. But if I've saved the above function as $sub$ then

sub2 = var('$sub$')
subX = sub(1,2)

causes an error:

> Trying to invoke a non-function 'string' @ line...

And

function subX(a,b){
    var('$sub$')
}

when sub only contains return a - b, doesn't work either.

Obviously I need to convert the string to a function but I'm not sure whether that's possible.

I know this is a bit of an obscure language but if anyone knows how this can be done in similar languages like JavaScript and PHP, I'm happy to test out any guesses...

答案1

得分: 0

这里的解决方案是移除function部分,直接输入脚本,这样会继承执行范围,所以如果我的全局变量$script$是:

return 'hello ' + a

然后我可以用以下方式执行该函数:

a = 'world'

value = exec(var('$script$'))
echo(value)

(感谢 Webhook.Site 的支持团队为解释此事)

英文:

The solution here is to remove the function section and just enter the script, which inherits the execution scope so if my global variable $script$ is:

return 'hello ' + a

Then I can execute the function with:

a = 'world'

value = exec(var('$script$'))
echo(value)

(credit to Webhook.Site's support team for explaining this)

huangapple
  • 本文由 发表于 2023年1月9日 01:09:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定