Using server-side javascript in classic asp: what's wrong with "this"?

looks like: Inserting objects into a global scope in classic ASP / Javascript


Trying to get started using javascript in classic ASP. It seems like it could be some kind of “mistake”: can someone with some experience in this tell me what is wrong with the code “Blah2”? It seems like this "should" work, but there seems to be a problem using "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> 

Thanks for any pointers

Tim

+4
source share
1 answer

You need partners around your function

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

Also, this.w does not do what you want. this actually points to a global object right there. Do you want to:

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

or

 bar Blah2 = new (function(){ ... 
+2
source

Source: https://habr.com/ru/post/1345019/


All Articles