我可以从VBScript初始化用JScript编写的对象吗?

时间:2009-11-01 15:13:47

标签: vbscript javascript wsh

我正在尝试编写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>

有没有办法完成我想要做的事情?

1 个答案:

答案 0 :(得分: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>