javascript - 在经典 asp : what's wrong with "this"? 中使用服务器端 javascript

标签 javascript asp-classic jscript

similar: Inserting objects into global scope in classic 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(){
   ...

关于javascript - 在经典 asp : what's wrong with "this"? 中使用服务器端 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5413384/

相关文章:

javascript - 从 checkbox1 中选择项目并从 dropdown1 中选择项目后,结果在 dropdown2 中显示/隐藏选项值

asp-classic - ASPupload 'if Not File Is Nothing' 始终返回 false

xml - 经典 asp 中的 System.Net.HttpWebRequest?

javascript - 如何修改JavaScript对象名称?

javascript - 如何删除选定的元素 react

asp-classic - 类型不匹配: 'Server.HTMLEncode' ASP

javascript - "FizzBuzz"样式代码的错误答案

javascript - 如何使用 jquery 在 html 上显示 ajax 列表响应?

javascript - AngularJS 添加表示数组模型的输入

javascript - 为什么这个javascript不断递归