Function vs. command?

You may have noticed that in some of our SSJS examples, we are writing functions named "function xyz()" and sometimes we are writing functions called "command xyz()" and you may be wondering what the difference is.

It's actually pretty simple. Commands can be called from the browser location bar in an URL and functions can't. In nearly all other respects they are identical.

This has some usage implications, though. If you want to add an extra level of security to a routine you wrote, name it as a function, and people won't be able to execute it from the browser directly.

And there are two more trivia facts you should know about functions and commands:

  • Some filters have to be named as commands or they won't work.
  • Commands can be called from WCTL with the usual WCTL "use" syntax rather than the more convoluted JS call syntax:

    %% use myCommand %%

    rather than

    %% "myCommand".jsCall() %%

And that's it. As you can see, in most cases it doesn't make a huge amount of difference which you use.

2 comments:

  1. Slight expansion of one point: Most filters are part of the WCTL infrastructure. If you code one of them in SSJS, it has to be a command. But other filters - notably the email filters - are built in to the SSJS infrastructure, and they can be functions.

    It's a historical oddity from the way the codebase developed.

    ReplyDelete
  2. Thanks, I've clarified that above.

    ReplyDelete