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

Tim*_*ams 5 javascript asp-classic jscript

类似:在经典的 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>
Run Code Online (Sandbox Code Playgroud)

感谢您的任何指点

蒂姆

小智 3

你的函数周围需要括号

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

还有就是this.w没有做自己想做的事。 this实际上指向那里的全局对象。你要:

var Blah2 = (function(){
    return {w : function(s){ Response.write(s); }};
}());
Run Code Online (Sandbox Code Playgroud)

或者

bar Blah2 = new (function(){
   ...
Run Code Online (Sandbox Code Playgroud)