Inserting objects into global scope in classic ASP / Javascript

后端 未结 1 986
粉色の甜心
粉色の甜心 2021-01-13 07:39

This question pertains to Javascript in classic ASP. It has nothing to do with Javascript running in browsers.

A typical construct for a JS module that is designed

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-13 08:10

    I'm not sure what the underlying type is but it will be some COM object. Unless this COM object implements IDispatchEx you will not able to assign arbitary properties to it. This is the case for the COM objects from MSHTML that underlies Internet Explorer's DHTML implementation. However it would appear that ASP has not provided the same feature.

    There is a work-around assuming that the parameter globalScope is truely expected to only ever be the global scope:

    (function() { 
       var data = ['Alpha', 'Beta', 'Gamma']; 
    
       function helper(a) { .... }  
    
       function search(d) { .... } 
    
       // "export" a function so it is externally visible 
       searchData = search;  
    
    })();   // Please not also small syntatic correction of your original code.
    

    With the caveat that the property searchData must not already be present anywhere up the scope chain. In this case JScript will create it at the global level.

    The name searchData does become a named item in the Active Script (i.e. if you were to also include some VBScript in the same page that VBScript can also see searchData). In addition this.searchData is now assigned. It would seem that whatever the global object is it allows late bound resolution of member names to be mapped to named items on the Active Script object itself.

    0 讨论(0)
提交回复
热议问题