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/