I know I can call top-level functions defined in JS from VBScript, and vice versa, like this:
<%@ language="Chakra" %>
<script language='JavaScript' runat='server'>
function jsFunction1() {
for (var i=0;i<10;i++) Response.Write(i+"<br>");
vbFunction2();
}
</script>
<script language='VBScript' runat='server'>
Sub vbFunction1 ()
Response.Write("VB Hello <br/>" & VbCrLf)
jsFunction1()
End Sub
Sub vbFunction2 ()
Response.Write("VB Goodbye <br/>" & VbCrLf)
End Sub
</script>
<script language="JavaScript" runat="server">
vbFunction1();
</script>
I can also include JS into VBScript modules, like this:
<%@ language="VBScript" %>
<script language="Javascript" runat="server" src="includedModule.js"></script>
<script language="VBScript" runat="server">
....
</script>
...and the functions defined in the includedModule.js are available in the VBScript.
But suppose I have a Javascript class defined using prototypal OO, like this:
(function() {
MyObj = function() {
this.foo = ...
...
};
MyObj.prototype.method1 = function() { .. };
MyObj.prototype.method2 = function() { .. };
}());
How can I use that object (aka type, or class) from VBScript?
The vanilla approach...
Dim foo
Set foo = New MyObj
...does not work.
Neither does
Dim foo
foo = MyObj()
...because apparently this
is not defined when the JS function is invoked
from VBScript. Or something.
So how can I do it?
The reason this is valuable: there are OO libraries available in Javascript, that would be interesting to use from VBScript.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…