英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论