类似:Inserting objects into global scope in classic ASP / Javascript
尝试开始在经典ASP中使用javascript。虽然看起来对此有些“陷阱”:任何有这方面经验的人都可以告诉我“Blah2”代码是什么吗?似乎它“应该”起作用,但我使用“这个”似乎有问题......
<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>
感谢您的任何指示
添
答案 0 :(得分:2)
你需要关于你的功能的parens
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(){
...