5

similar: Inserting objects into global scope in classic ASP / Javascript


Trying to get started with using javascript in classic ASP. Seem to be some "gotchas" with this though: can anyone with some experience in this tell me what's up with the "Blah2" code ? Seems like it "should" work but there seems to be a problem with my use of "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

Community
  • 1
  • 1
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • 1
    @Sebastian: Classic ASP allowed you to use either VBScript or JavaScript on the server-side. – John Saunders Mar 24 '11 at 01:13
  • 1
    you probably realize this, but believe it or not, there are some who don't - "Classic" ASP shouldn't be used unless you have no choice. – John Saunders Mar 24 '11 at 01:13
  • @John: Yes, I know that by now I should be up to speed with asp.net (after all, it's at least 10 years old at this point). However, I'm pretty familiar with classic ASP, have a quick project to push out, and thought I'd at least try to learn something new (old?) by switching from server-side vbscript to j(ava)script, to brush up on a bit of JSON. – Tim Williams Mar 24 '11 at 04:34
  • `runat="server"` is for asp.net, not classic asp. which were you really using? – Joel Coehoorn Nov 22 '11 at 00:47
  • 5
    @Joel: I was really using classic ASP. Classic ASP also uses runat=server... – Tim Williams Nov 22 '11 at 01:12
  • 2
    It's funny how many people insist Classic ASP can't run javascript server-side... – ErikE Oct 22 '12 at 22:04

1 Answers1

3

You need parens 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 isn't doing what you want. this is actually pointing to the global object right there. You want:

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

Or

bar Blah2 = new (function(){
   ...
ErikE
  • 48,881
  • 23
  • 151
  • 196
  • Someone else please jump in and correct me if I'm wrong, but I'm pretty sure "this" in the context I used it refers to the function Blah2, not to the global object... At least, that's how it works in a browser: what I'm not clear about is why it doesn't work in this context. Unless you're specifically saying *in server-side ASP* it works as you describe ? – Tim Williams Mar 24 '11 at 04:40
  • Nope, not unless you create an object with `new`. Run this in any browser if you don't believe me: `function blah(){ alert(this === window); } blah()`. Function calls don't, by themselves, have `this` set. –  Mar 24 '11 at 04:42
  • I should add that your second example (similar to my first) *does* work. I'm really just trying to find out why "this" seems to be a problem in this context. – Tim Williams Mar 24 '11 at 04:44
  • thanks for correcting me: I just read another SO question addressing this and headed back here to eat my words. – Tim Williams Mar 24 '11 at 05:04
  • 1
    javascript is **case sensitive**, folks! Don't forget this when mixing vbscript and javascript server-side. – ErikE Oct 22 '12 at 22:05