How can I use Javascript OO classes from VBScript, in an ASP-Classic or WSH environment?

后端 未结 1 2076
挽巷
挽巷 2020-12-21 18:52

I know I can call top-level functions defined in JS from VBScript, and vice versa, like this:

<%@ language=\"Chakra\" %>



        
相关标签:
1条回答
  • 2020-12-21 19:18

    I don't know how to avoid the problem that VBScript cannot directly call a Javascript "constructor" function. The way I dealt with it was to simply define a shim: a top-level function in Javascript that invokes the constructor from within Javascript and returns the reference.

    So:

    <script language='javascript' runat='server'>(function() {  
      MyObj = function() {  
        this.foo = ...  
        ...  
      };  
    
      MyObj.prototype.method1 = function() { .. };  
      MyObj.prototype.method2 = function() { .. };  
    
      // define a shim that is accessible to vbscript
      Shim = {construct: function() { return new MyObj(); } };
    
    }());  
    </script>
    
    <script language='vbscript' runat='server'>
      Dim foo
      Set foo = Shim.construct()
       ...
    </script>
    
    0 讨论(0)
提交回复
热议问题