插入对象在传统的ASP / JavaScript的全局作用域 [英] Inserting objects into global scope in classic ASP / Javascript

查看:128
本文介绍了插入对象在传统的ASP / JavaScript的全局作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题涉及到的Javascript在传统的ASP。它无关的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));

这使得code进行语法封装,以允许在运行时的解析器/编译器检查。它还提供了范围管理,让增值经销商和花括号内声明的函数将不可见的外部。

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"

这是所有pretty典型。

This is all pretty typical.

在使用这种传统的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?

在浏览器中运行时,本的计算结果为窗口。在服务器端的传统的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" ?

推荐答案

我不知道底层的类型是什么,但它会出现一些COM对象。除非这个COM对象实现 IDispatchEx 您将无法arbitary属性分配给它。这是从MSHTML该underlies Internet Explorer的DHTML实现COM对象的情况。但是它会出现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 不得已经是present任何地方了作用域链。在这种情况下的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 并成为活动脚本一个命名项(例如,如果你要还包括一些VBScript中在同一个页面,VBScript中还可以看到 searchData )。此外 this.searchData 现在被指定。这似乎是任何全局对象是它允许成员名称的后期绑定的分辨率被映射到Active脚本对象本身的命名项。

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天全站免登陆