If I have a function in SSJS and I want to pass one "firm" parameter and a list of others that can change, what's the best way to do that? With some kind of hashMap or JSON or something else?
如果我在SSJS中有一个函数,我想传递一个“firm”参数和一个可以更改的列表,那么最好的方法是什么?用一些hashMap或JSON之类的东西?
for example given something like:
例如:
myfunction( code:string, paramList:??) { // do stuff here
myfunction(代码:string, paramList:?){//做这些事情
}
}
Basically the function will create a document. And sometimes I'll have certain fields I'll want to pass in right away and populate and other times I'll have different fields I will want to populate.
基本上,函数将创建一个文档。有时我会有一些字段我想马上传入并填充另一些时候我会有不同的字段我想填充。
How would you pass them in and then parse out in the function?
如何将它们传递进来然后在函数中解析?
Thanks!
谢谢!
3 个解决方案
#1
5
Use the arguments parameter... In JavaScript you are not required to define any of your parameters in the function block itself. So, for example, the following call:
使用参数参数…在JavaScript中,不需要在函数块本身中定义任何参数。例如,下面的调用:
myFunction(arg1, arg2, arg3, arg4);
can legally be passed to the following function:
可合法地传递给下列职能:
myFunction () {
// do stuff here...
}
when I do this, I usually place a comment in the parens to indicate I am expecting variable arguments:
当我这样做的时候,我通常会在括号里放一个注释来表明我在期待变量参数:
myFunction (/* I am expecting variable arguments to be passed here */) {
// do stuff here...
}
Then, you can access those arguments like this:
然后,你可以像这样访问这些参数:
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;
}
Wallah, variable arguments.
要人,变量参数。
But, more to the point of your question, I don't think you need to actually send variable arguments, nor go through the hassle of creating actual JSON (which is really a string interpretation of a javascript object), just create and send the actual object then reference as an associative array to get your field names and field values:
但更重要的是你的问题,我认为你不需要发送变量参数,也没有经过实际创建JSON的麻烦(这真的是一个字符串的解释一个javascript对象),创建和发送实际的对象然后引用作为一个关联数组中让你的字段名和字段值:
var x = {};
x.fieldName1 = value1;
x.fieldName2 = value2;
// ... etc ...
then in your function, which now needs only two parameters:
然后在函数中,现在只需要两个参数:
myFunction(arg1, arg2) {
// do whatever with arg1
for (name in arg2) {
// name is now "fieldName1" or "fieldName2"
alert(name + ": " + x[name]);
}
}
Hope this helps.
希望这个有帮助。
#2
5
I would do this with a JSON object as the second parameter...
我将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);
}
Then you call the function like this:
然后你这样调用函数:
nyfunction("somecode", {form:"SomeForm", subject:"Whatever",uname:@UserName()});
Happy coding.
快乐的编码。
/Newbs
/新手
#3
-3
I don't think that is possible in SSJS. I think the best option you have is to pass a hashmap or your own (java) object. I think a custom java object would be the nicest option because you can define some 'structure' on how your function can process it. A hashmap can be easily extended but it is not easy if you have a lot of code that create a lot of different hashmap structures...
我认为这在SSJS中是不可能的。我认为最好的选择是传递一个hashmap或您自己的(java)对象。我认为自定义java对象将是最好的选择,因为您可以定义一些“结构”来说明函数如何处理它。hashmap可以很容易地扩展,但是如果您有许多代码来创建许多不同的hashmap结构,那么就不容易了……
#1
5
Use the arguments parameter... In JavaScript you are not required to define any of your parameters in the function block itself. So, for example, the following call:
使用参数参数…在JavaScript中,不需要在函数块本身中定义任何参数。例如,下面的调用:
myFunction(arg1, arg2, arg3, arg4);
can legally be passed to the following function:
可合法地传递给下列职能:
myFunction () {
// do stuff here...
}
when I do this, I usually place a comment in the parens to indicate I am expecting variable arguments:
当我这样做的时候,我通常会在括号里放一个注释来表明我在期待变量参数:
myFunction (/* I am expecting variable arguments to be passed here */) {
// do stuff here...
}
Then, you can access those arguments like this:
然后,你可以像这样访问这些参数:
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;
}
Wallah, variable arguments.
要人,变量参数。
But, more to the point of your question, I don't think you need to actually send variable arguments, nor go through the hassle of creating actual JSON (which is really a string interpretation of a javascript object), just create and send the actual object then reference as an associative array to get your field names and field values:
但更重要的是你的问题,我认为你不需要发送变量参数,也没有经过实际创建JSON的麻烦(这真的是一个字符串的解释一个javascript对象),创建和发送实际的对象然后引用作为一个关联数组中让你的字段名和字段值:
var x = {};
x.fieldName1 = value1;
x.fieldName2 = value2;
// ... etc ...
then in your function, which now needs only two parameters:
然后在函数中,现在只需要两个参数:
myFunction(arg1, arg2) {
// do whatever with arg1
for (name in arg2) {
// name is now "fieldName1" or "fieldName2"
alert(name + ": " + x[name]);
}
}
Hope this helps.
希望这个有帮助。
#2
5
I would do this with a JSON object as the second parameter...
我将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);
}
Then you call the function like this:
然后你这样调用函数:
nyfunction("somecode", {form:"SomeForm", subject:"Whatever",uname:@UserName()});
Happy coding.
快乐的编码。
/Newbs
/新手
#3
-3
I don't think that is possible in SSJS. I think the best option you have is to pass a hashmap or your own (java) object. I think a custom java object would be the nicest option because you can define some 'structure' on how your function can process it. A hashmap can be easily extended but it is not easy if you have a lot of code that create a lot of different hashmap structures...
我认为这在SSJS中是不可能的。我认为最好的选择是传递一个hashmap或您自己的(java)对象。我认为自定义java对象将是最好的选择,因为您可以定义一些“结构”来说明函数如何处理它。hashmap可以很容易地扩展,但是如果您有许多代码来创建许多不同的hashmap结构,那么就不容易了……