繁体   English   中英

在经典的ASP中使用服务器端JavaScript:“ this”有什么问题?

[英]Using server-side javascript in classic asp: what's wrong with “this”?

相似: 在经典的ASP / Javascript中将对象插入全局范围


尝试开始在经典ASP中使用javascript。 不过,这似乎有些“陷阱”:任何有一定经验的人都可以告诉我“ Blah2”代码的最新情况吗? 似乎“应该”起作用,但是我对“ this”的使用似乎存在问题。

<script language="javascript" runat="server">

 var Blah = {};
 Blah.w = function(s){Response.write(s);}

 Blah.w('hello'); //this works...


 var Blah2 = function(){
     this.w = function(s){Response.write(s);} 
     //line above gives 'Object doesn't support this property or method'
     return this;
 }();

 Blah2.w('hello');

</script>

谢谢你的指导

提姆

您需要在功能周围添加括号

var Blah2 = (function(){
    this.w = function(s){Response.write(s);} 
    //line above gives 'Object doesn't support this property or method'
    return this;
}());

另外, this.w并未执行您想要的操作。 this实际上是指向那里的全局对象。 你要:

var Blah2 = (function(){
    return {w : function(s){ Response.write(s); }};
}());

要么

bar Blah2 = new (function(){
   ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM