Home > Software design >  Retrieving A Function From A WebhookScript Global Variable
Retrieving A Function From A WebhookScript Global Variable

Time:01-11

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...

CodePudding user response:

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)

  • Related