Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting objects into global scope in classic ASP / Javascript

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 to be re-usable is like this:

(function(globalScope) {
   ... declarations here...
}(this));

This allows the code to be syntactically encapsulated, to allow checks by the run-time parser/compiler. It also provides scope management, so that vars and functions declared within the curlies will not be visible externally.

Another typical construct is to "export" an object or function belonging to the inner scope, to the outer scope, via assignment, like this:

(function(globalScope) {
   var data = ['Alpha', 'Beta', 'Gamma'];

   function helper(a) { .... } 

   function search(d) { .... }

   // "export" a function so it is externally visible
   globalScope.searchData = search; 

}(this));

// typeof this.searchData == "function" 
// typeof this.data == "undefined"
// typeof this.helper == "undefined"
// typeof this.search == "undefined"

This is all pretty typical.

When using this sort of construct in classic ASP (attention: server-side javascript!!) the JS engine throws up. I get a 500 error.

Why?

Can I use the scoping construct and "export" things to the global scope, in classic ASP?

In a browser runtime, "this" evaluates to "window". In a server-side classic ASP runtime, what is the value of the global "this"? Is it possible to assign new properties to that "this" ?

like image 967
Cheeso Avatar asked Nov 04 '22 04:11

Cheeso


1 Answers

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.

like image 78
AnthonyWJones Avatar answered Nov 17 '22 06:11

AnthonyWJones