Loading TOC...


   function as xs.QName?,
   [module-path as String?]
) as function


Returns a function value as an xdmp.function type. You can return an xdmp.function from an expression or a function. You can execute the function referred to by an xdmp.function by passing the xdmp.function value to xdmp.apply. If the module-path ends with a file extension matching the ones configured for application/vnd.marklogic-javascript in your MarkLogic Mimetypes configuration, and the function's namespace URI is empty, the module is considered to be JavaScript. In this case, the function parameter can be empty.

function The function QName, which includes its local name and namespace. If the function is not found in the current query context or in the module specified in the second parameter, then an exception is thrown when the function is used with xdmp.apply.
module-path The optional path to the module where the function specified in the first parameter is defined. If the module-path is not supplied, the function QName must be in-scope in the query context. If the empty sequence is supplied, the function behaves as if the parameter is not supplied (that is, it uses the in-scope query context).




var f = xdmp.function(xs.QName("fn:concat"));
xdmp.apply(f, "hello", " world");

=> hello world


// Given square.sjs in the modules database as the following:

module.exports = function(width) {
  return {
    area: function() {
      return width * width;
    set: function(_width) {
      width = _width;
// then you can run the following
var mySquare = fn.head(xdmp.apply(xdmp.function(null,"square.sjs"),4));
xdmp.apply(mySquare.set, 3);

=> 9

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.