Can I initialize objects written in JScript from V

2020-03-23 02:51发布

问题:

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?

回答1:

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>