在 JavaScript 中使用“输出”参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5122704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Working with "Out" Parameters in JavaScript
提问by RunnerRick
I am working with an ActiveX control in Internet Explorer 8 that is to display a save file dialog which let's the user choose a file name and file type (jpg, gif, etc). These values get passed to code and then are used in a different method to save the file. Unfortunately the method that invokes the dialog has no return value, and the file name and file type are passed in as outparameters.
我正在使用 Internet Explorer 8 中的 ActiveX 控件,该控件将显示一个保存文件对话框,让用户选择文件名和文件类型(jpg、gif 等)。这些值被传递给代码,然后以不同的方法用于保存文件。不幸的是,调用对话框的方法没有返回值,文件名和文件类型作为输出参数传入。
The signature of the method (expressed in Visual Basic) looks like this:
该方法的签名(用 Visual Basic 表示)如下所示:
Public Sub SaveFileDialog( _
ByVal bstrDialogType As Variant, _
ByRef pbstrFileName As String, _
ByRef out_pvType As Long _
)
The two ByRefparameters are the outparameters.
两个ByRef参数是输出参数。
I have written the following JavaScript code:
我编写了以下 JavaScript 代码:
try
{
var saveFileName, saveFileType; // out variables
gxVideoPlayBack.SaveFileDialog("image", saveFileName, saveFileType);
alert(saveFileName); // displays "undefined"
alert(saveFileType); // displays "undefined"
}
catch(error)
{
if(!error.number === -2147221484) // User clicked cancel.
{
alert(error.message);
}
}
The code works in that the ActiveX control produces its dialog, and I can handle error conditions, but I can't seem to figure out how to capture the values of the outparameters.
该代码的工作原理是 ActiveX 控件生成其对话框,并且我可以处理错误情况,但我似乎无法弄清楚如何捕获输出参数的值。
In the code gxVideoPlayBackis a reference to the ActiveX control embedded in the DOM via an HTML element.
在代码中gxVideoPlayBack是对通过 HTML 元素嵌入到 DOM 中的 ActiveX 控件的引用。
If JavaScript will not work for this, can it be done in VBScript?
如果 JavaScript 对此不起作用,是否可以在 VBScript 中完成?
As an alternative I can just implement my own dialog, but would rather use the one provided.
作为替代方案,我可以实现自己的对话框,但宁愿使用提供的对话框。
采纳答案by maerics
Edit:It seems that it's not possible to have "out" parameters in JavaScript/JScript.
编辑:似乎在 JavaScript/JScript 中不可能有“out”参数。
Original:Perhaps the approach described in this articlewill work:
var saveFileName={}, saveFileType={}; // Empty "output" objects.
gxVideoPlayBack.SaveFileDialog("image", saveFileName, saveFileType);
alert(saveFileName.value); // The "value" attribute is assigned ...
alert(saveFileType.value); // ... by the "SaveFileDialog" method?
I suppose the idea is that the WSH wrapper for this native call will attempt to assign the "value" property of the given output parameters, so you can either override the value setter or just give it an object with a built-in value setter.
我想这个想法是这个本机调用的 WSH 包装器将尝试分配给定输出参数的“值”属性,因此您可以覆盖值设置器或只给它一个带有内置值设置器的对象。
回答by Wayne
All function arguments in JavaScript are passed by value(even if the valuebeing passed is a referenceto an object (which it is)). There is no pass-by-reference.
在JavaScript中所有函数参数传递由值(即使该值被传递是一个参考的一个对象(它是))。没有传递引用。
If SaveFileDialogmodifies the objects referenced by saveFileNameand saveFileTypethen you have access to those changes through your existing variables.
如果SaveFileDialog修改了引用的对象saveFileName,saveFileType然后您可以通过现有变量访问这些更改。
回答by Martijn
Unfortunately, out/ByRef parameters will only work in JScript for objects; not for any other type (numbers, strings).
不幸的是,out/ByRef 参数只能在 JScript 中用于对象;不适用于任何其他类型(数字、字符串)。
In this case, you'll have to use VBScript, which does support ByRef arguments, or like maerics says, write a VB/VBScript wrapper for the SaveFileDialog method, which could return an object containing both file name and type.
在这种情况下,您必须使用支持 ByRef 参数的 VBScript,或者像 maerics 所说的那样,为 SaveFileDialog 方法编写一个 VB/VBScript 包装器,它可以返回一个包含文件名和类型的对象。

