在经典 ASP/Javascript 中将对象插入全局范围 [英] Inserting objects into global scope in classic ASP / Javascript

查看:13
本文介绍了在经典 ASP/Javascript 中将对象插入全局范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与经典 ASP 中的 Javascript 有关.它与在浏览器中运行的 Javascript 无关.

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

设计为可重用的 JS 模块的典型构造如下:

A typical construct for a JS module that is designed to be re-usable is like this:

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

这允许在语法上封装代码,以允许运行时解析器/编译器进行检查.它还提供范围管理,因此在 curlies 中声明的变量和函数将不会在外部可见.

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"

这一切都很典型.

当在经典 ASP 中使用这种结构时(注意:服务器端 javascript!!),JS 引擎会报错.我收到 500 错误.

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

为什么?

我可以在经典 ASP 中使用范围构造并将事物导出"到全局范围吗?

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

在浏览器运行时,this"的计算结果为window".在服务器端经典 ASP 运行时中,全局this"的值是多少?是否可以为this"分配新属性?

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" ?

推荐答案

我不确定底层类型是什么,但它会是一些 COM 对象.除非此 COM 对象实现 IDispatchEx,否则您将无法为其分配任意属性.来自 MSHTML 的 COM 对象就是这种情况,它是 Internet Explorer 的 DHTML 实现的基础.但是,ASP 似乎没有提供相同的功能.

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.

假设参数 globalScope 确实应该只是全局范围,有一个变通方法:

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.

警告 属性 searchData 不得已出现在作用域链的任何位置.在这种情况下,JScript 将在全局级别创建它.

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.

名称 searchData 确实成为 Active Script 中的命名项目(即,如果您还要在同一页面中包含一些 VBScript,VBScript 也可以看到 searchData).此外,现在分配了 this.searchData.似乎无论全局对象是什么,它都允许将成员名称的后期绑定解析映射到 Active Script 对象本身上的命名项.

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.

这篇关于在经典 ASP/Javascript 中将对象插入全局范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆