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

Dav*_*edy 1 xpages

如果我在SSJS中有一个函数,并且我想传递一个"公司"参数和一个可以改变的其他参数列表,那么最好的方法是什么?使用某种hashMap或JSON或其他东西?

举例来说:

myfunction(code:string,paramList:??){//在这里做的东西

}

基本上该函数将创建一个文档.有时我会有一些我想要立即传递的字段并填充其他时间我会有不同的字段我想要填充.

你将如何传递它们然后在函数中解析?

谢谢!

New*_*wbs 5

我会用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);
}
Run Code Online (Sandbox Code Playgroud)

然后你调用这样的函数:

nyfunction("somecode", {form:"SomeForm", subject:"Whatever",uname:@UserName()});
Run Code Online (Sandbox Code Playgroud)

快乐的编码.

/ Newbs


Jer*_*dge 5

使用arguments参数...在JavaScript中,您不需要在功能块本身中定义任何参数.因此,例如,以下调用:

myFunction(arg1, arg2, arg3, arg4);
Run Code Online (Sandbox Code Playgroud)

可以合法地传递给以下函数:

myFunction () {
  // do stuff here...
}
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我通常在parens中发表评论以表明我期待变量参数:

myFunction (/* I am expecting variable arguments to be passed here */) {
  // do stuff here...
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以像这样访问这些参数:

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;
}
Run Code Online (Sandbox Code Playgroud)

沃拉,可变论点.

但是,更多的是你的问题,我认为你不需要实际发送变量参数,也不需要经历创建实际JSON(这实际上是javascript对象的字符串解释)的麻烦,只需创建并发送然后将实际对象引用为关联数组以获取字段名称和字段值:

var x = {};
x.fieldName1 = value1;
x.fieldName2 = value2;
// ... etc ...
Run Code Online (Sandbox Code Playgroud)

然后在你的函数中,现在只需要两个参数:

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

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

}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.