Referencing Another Script Component in the Same Package
You can create a package that contains multiple Windows® Script Components, so you can also instantiate and use one of the script components from the other without having to register the second script component.
Scenario
For example, you might create one script component that implements the Automation interface and exposes a series of properties and methods. A second script component in the same package might implement the ASP interface, which provides access to the server object model for Microsoft® Internet Information Services (IIS). You can then create a method or property in the Automation script component that exposes the ASP script component and makes its members available.
To reference one script component from another, create a skeleton member — a property or method — in one method that exposes the second script component.
To reference another script component in the same script component file
Declare a property or method in the first script component.
As part of the definition for the new property or method, call the createComponent function.
For example, the following shows two script components in the same package. In the first script component, the math method simply references the second script component, which exposes the add method and the multiply method.
Note
A CDATA section is required to make the script in the <script> element opaque. For details, see Script Component Files and XML Conformance.
<?XML version="1.0"?> <package> <component id="component1"> <registration progid="Component.FrontEnd"/> <public> <property name="math"/> </public> <script language="JScript"> <![CDATA[ var math = createComponent("component2") ]]> </script> </component> <component id="component2"> <registration progid="Component.Math"/> <public> <method name="add"/> <method name="multiply"/> </public> <script language="JScript"> <![CDATA[ function add(n1, n2){ return n1+n2; } function multiply(n1, n2){ return n1*n2; } ]]> </script> </component> </package>
To invoke the referenced script component, call the full member hierarchy to get to its methods or properties. The following example illustrates a few ways to do this:
' Creates instance of first script component.
set o1 = CreateObject("Component.FrontEnd")
' Invokes the second script component's functions directly.
msgbox(o1.math.add(3,5))
msgbox(o1.math.multiply(3,5))
' Creates a second object that references the math method directly.
Set o2 = o1.math()
msgbox(o2.add(4,5))
msgbox(o2.multiply(4,5))
Each time you call the createComponent() function, it creates a new instance of the referenced script component. If you need to preserve instance information between calls, store the pointer to the second script component in a global variable, as in the following example.
Note
A CDATA section is required to make the script in the <script> element opaque. For details, see Script Component Files and XML Conformance.
<component id="component1">
<registration progid="Component.FrontEnd"/>
<public>
<property name="math">
<get/>
</property>
</public>
<script language="JScript">
<![CDATA[
var m = createComponent("component2")
function get_math(){
return m
}
]]>
</script>
</component>
(Component2 as in previous example)
See Also
Concepts
Script Component File Contents