如何将变量参数传递给XPages SSJS函数?

如何将变量参数传递给XPages SSJS函数?,xpages,Xpages,如果我在SSJS中有一个函数,我想传递一个“固定”参数和一个可以更改的其他参数列表,那么最好的方法是什么?使用某种hashMap或JSON或其他东西 例如: myfunction(代码:字符串,参数列表:??){ //在这里做事 } 基本上,该函数将创建一个文档。有时我会有一些字段,我想马上传递并填充,有时我会有不同的字段,我想填充 如何在函数中传入和解析它们 谢谢 我认为这在SSJS中是不可能的。我认为您最好的选择是传递一个hashmap或您自己的(java)对象。我认为自定义java对象是最

如果我在SSJS中有一个函数,我想传递一个“固定”参数和一个可以更改的其他参数列表,那么最好的方法是什么?使用某种hashMap或JSON或其他东西

例如:

myfunction(代码:字符串,参数列表:??){ //在这里做事

}

基本上,该函数将创建一个文档。有时我会有一些字段,我想马上传递并填充,有时我会有不同的字段,我想填充

如何在函数中传入和解析它们


谢谢

我认为这在SSJS中是不可能的。我认为您最好的选择是传递一个hashmap或您自己的(java)对象。我认为自定义java对象是最好的选择,因为您可以定义一些函数如何处理它的“结构”。hashmap可以很容易地扩展,但是如果有很多代码创建了很多不同的hashmap结构,那么扩展就不容易了…

我认为这在SSJS中是不可能的。我认为您最好的选择是传递一个hashmap或您自己的(java)对象。我认为自定义java对象是最好的选择,因为您可以定义一些函数如何处理它的“结构”。hashmap可以很容易地扩展,但是如果有很多代码创建了很多不同的hashmap结构,那么扩展就不容易了……

我会使用JSON对象作为第二个参数来完成这项工作

function myfunction(code:String, data) {
   // do stuff here...
   var doc:NotesDocument = database.CreateDocument();
   if(data) {
      for (x in data) {
         doc.replaceItemValue(x, data[x]);
      }
   }
   // do more stuff
   doc.save(true, false);
}
然后调用如下函数:

nyfunction("somecode", {form:"SomeForm", subject:"Whatever",uname:@UserName()});
myFunction (/* I am expecting variable arguments to be passed here */) {
  if (arguments.length == 0) {
    // naughty naughty, you were supposed to send me things...
    return null;
  }

  myExpectedFirstArgument = arguments[0];

  // maybe do something here with myExpectedFirstArgument
  var whatEvah:String = myExpectedFirstArgument + ":  "

  for (i=1;i<arguments.length;i++) {
    // now do something with the rest of the arguments, one 
    // at a time using arguments[i]
    whatEvah = whatEvah + " and " + arguments[i];
  }

  // peace.
  return whatEvah;
}
快乐编码


/Newbs

我将使用JSON对象作为第二个参数来完成此操作

function myfunction(code:String, data) {
   // do stuff here...
   var doc:NotesDocument = database.CreateDocument();
   if(data) {
      for (x in data) {
         doc.replaceItemValue(x, data[x]);
      }
   }
   // do more stuff
   doc.save(true, false);
}
然后调用如下函数:

nyfunction("somecode", {form:"SomeForm", subject:"Whatever",uname:@UserName()});
myFunction (/* I am expecting variable arguments to be passed here */) {
  if (arguments.length == 0) {
    // naughty naughty, you were supposed to send me things...
    return null;
  }

  myExpectedFirstArgument = arguments[0];

  // maybe do something here with myExpectedFirstArgument
  var whatEvah:String = myExpectedFirstArgument + ":  "

  for (i=1;i<arguments.length;i++) {
    // now do something with the rest of the arguments, one 
    // at a time using arguments[i]
    whatEvah = whatEvah + " and " + arguments[i];
  }

  // peace.
  return whatEvah;
}
快乐编码


/新手使用arguments参数。。。在JavaScript中,不需要在函数块本身中定义任何参数。例如,下面的调用:

myFunction(arg1, arg2, arg3, arg4);
可以合法地传递给以下函数:

myFunction () {
  // do stuff here...
}
在执行此操作时,我通常会在参数栏中添加注释,以表明我需要变量参数:

myFunction (/* I am expecting variable arguments to be passed here */) {
  // do stuff here...
}
然后,您可以像这样访问这些参数:

nyfunction("somecode", {form:"SomeForm", subject:"Whatever",uname:@UserName()});
myFunction (/* I am expecting variable arguments to be passed here */) {
  if (arguments.length == 0) {
    // naughty naughty, you were supposed to send me things...
    return null;
  }

  myExpectedFirstArgument = arguments[0];

  // maybe do something here with myExpectedFirstArgument
  var whatEvah:String = myExpectedFirstArgument + ":  "

  for (i=1;i<arguments.length;i++) {
    // now do something with the rest of the arguments, one 
    // at a time using arguments[i]
    whatEvah = whatEvah + " and " + arguments[i];
  }

  // peace.
  return whatEvah;
}
然后在函数中,现在只需要两个参数:

myFunction(arg1, arg2) {
   // do whatever with arg1

   for (name in arg2) {
     // name is now "fieldName1" or "fieldName2"
     alert(name + ": " + x[name]);
   }

}

希望这有帮助。

使用arguments参数。。。在JavaScript中,不需要在函数块本身中定义任何参数。例如,下面的调用:

myFunction(arg1, arg2, arg3, arg4);
可以合法地传递给以下函数:

myFunction () {
  // do stuff here...
}
在执行此操作时,我通常会在参数栏中添加注释,以表明我需要变量参数:

myFunction (/* I am expecting variable arguments to be passed here */) {
  // do stuff here...
}
然后,您可以像这样访问这些参数:

nyfunction("somecode", {form:"SomeForm", subject:"Whatever",uname:@UserName()});
myFunction (/* I am expecting variable arguments to be passed here */) {
  if (arguments.length == 0) {
    // naughty naughty, you were supposed to send me things...
    return null;
  }

  myExpectedFirstArgument = arguments[0];

  // maybe do something here with myExpectedFirstArgument
  var whatEvah:String = myExpectedFirstArgument + ":  "

  for (i=1;i<arguments.length;i++) {
    // now do something with the rest of the arguments, one 
    // at a time using arguments[i]
    whatEvah = whatEvah + " and " + arguments[i];
  }

  // peace.
  return whatEvah;
}
然后在函数中,现在只需要两个参数:

myFunction(arg1, arg2) {
   // do whatever with arg1

   for (name in arg2) {
     // name is now "fieldName1" or "fieldName2"
     alert(name + ": " + x[name]);
   }

}

希望这有帮助。

在javascript中,变量参数是可能的。在javascript中:可以,但问题是关于SSJS的。给出的答案是javascript。我以前在SSJS中尝试过,但没有成功。SSJS和JavaScript都基于ECMAScript,下面的代码对我有用:XPages Code:SSJS Code:function outputMultipleParams(){var output:string=”“;for(i=0;iIt是可能的,变量参数在javascript中是可能的。在javascript中:可以,但问题是关于SSJS的。给出的答案是在javascript中。我以前在SSJS中尝试过,但没有成功。SSJS和javascript都基于ECMAScript-以下代码对我有效:XPages代码:SSJS代码:function outputMultipleParams(){var output:string=“”;for(i=0;我在下面发布了我的示例后,详细阅读了您的示例-我被您说的JSON对象吓坏了…它不是真正的“JSON对象”,它是一个javascript对象…JSON是一个对象的字符串表示形式--所以本质上我和你的答案是一样的。谢谢Henry!这与Jeremy的答案非常相似。你打败了他,但我喜欢他关于先创建对象的那一部分。感觉有点像LotusScript类型。所以我将他的答案标记为,并给你买一只蜜蜂r下次我再见到你的时候。每个人都赢了!:-)在我把我的例子贴在下面之后,请仔细阅读你的例子-你说的JSON对象让我很反感…它不是真正的“JSON对象”,它是一个javascript对象…JSON是一个对象的字符串表示形式--所以本质上我和你的答案是一样的。谢谢Henry!这与Jeremy的答案非常相似。你打败了他,但我喜欢他关于先创建对象的那一部分。感觉有点像LotusScript类型。所以我将他的答案标记为,并给你买一只蜜蜂r下次我见到你。每个人都赢了!:-)谢谢Jeremy。我真的很喜欢先创建对象并传入。我现在也会在其他地方使用它。谢谢Jeremy。我真的很喜欢先创建对象并传入。我现在也会在其他地方使用它。