我可以初始化从VBScript用JScript编写的对象吗? [英] Can I initialize objects written in JScript from VBScript?

查看:54
本文介绍了我可以初始化从VBScript用JScript编写的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write a WSH logon script. Administrators throughout the company need to be able to customize the execution of the script, and execute additional scripts, for specific locations and users. In order to make their jobs easier, I would like to provide an API that the administrators can access in their scripts. If I write my API using JScript, would it be possible to initialize the objects I define through VBScript? For example, consider the following code:

<!-- 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>

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

Is there any way to accomplish what I'm trying to do?

推荐答案

VBScript和JScript在如何初始化对象方面似乎意见不一致.但是,一旦对象已初始化,则两种语言都可以识别它.为了解决这个问题,我必须在JScript中创建对象,然后将其返回给VBScript调用程序,如下所示.

VBScript and JScript seem to disagree on how to initialize an object. However, once the object has been initialized it is recognized by both languages. To get around this I had to create the object in JScript and then return it to the VBScript caller, as demonstrated below.

<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>

这篇关于我可以初始化从VBScript用JScript编写的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆