5

我正在尝试编写 WSH 登录脚本。整个公司的管理员需要能够为特定位置和用户自定义脚本的执行,并执行其他脚本。为了让他们的工作更轻松,我想提供一个 API,管理员可以在他们的脚本中访问。如果我使用 JScript 编写我的 API,是否可以初始化我通过 VBScript 定义的对象?例如,考虑以下代码:

<!-- The WSF logon script file -->
<package>
    <job>
        <script language="JScript">
            // A demonstration function
            function OverNineThousand() {
                return 9001;
            }

            // A demonstration "class"
            function WorkstationClass() {
                var os = "Windows XP";

                this.getOperatingSystem = function() {
                    return os;
                }
            }
        </script>

        <script language="VBScript">
            Dim bigNumber, workstation

            '// This assignment works properly.
            bigNumber = OverNineThousand() 

            '// This assignment causes an error. Am I doing it wrong?
            Set workstation = New WorkstationClass()

            '// Execution never gets this far
            WScript.Echo workstation.getOperatingSystem()
        </script>
    </job>
</package>

有什么办法可以完成我想做的事情吗?

4

1 回答 1

7

VBScript 和 JScript 似乎在如何初始化对象上存在分歧。但是,一旦对象被初始化,两种语言都可以识别它。为了解决这个问题,我必须在 JScript 中创建对象,然后将其返回给 VBScript 调用者,如下所示。

<package>
    <job>
        <script language="JScript">
            // A demonstration "class"
            function WorkstationClass() {
                var os = "Windows XP";

                this.getOperatingSystem = function() {
                    return os;
                }
            }

            function CreateWorkstation() {
                return new WorkstationClass();
            }
        </script>

        <script language="VBScript">
            Dim workstation

            '// This assignment causes an error.
            '// Set workstation = New WorkstationClass()

            '// This works!
            Set workstation = CreateWorkstation()

            '// Prints "Windows XP"
            WScript.Echo workstation.getOperatingSystem()
        </script>
    </job>
</package>
于 2009-11-01T18:51:42.953 回答