Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > OO javascript in ASP

Reply
Thread Tools

OO javascript in ASP

 
 
Jonathan Dodds
Guest
Posts: n/a
 
      03-18-2005
In "VBScript and JScript Don't Mix, at least in ASP"
<http://blogs.msdn.com/ericlippert/contact.aspx> Eric Lippert wrote:

Ideally you want the server side <SCRIPT> blocks to contain only global
function definitions, and the <% %> blocks to contain only "inline" code.


Why? Is this a convention or is there a difference between <script> element
blocks and <% %> blocks that make the former better for functions and the
latter better for inline?

I'm trying to use object-oriented JavaScript in my ASP application. I have a
constructor function and some prototype definitions. Here's an example from
the JScript documentation:

// circle.inc

function Circle (xPoint, yPoint, radius) {
this.x = xPoint; // The x component of the center of the circle.
this.y = yPoint; // The y component of the center of the circle.
this.r = radius; // The radius of the circle.
}
Circle.prototype.pi = Math.PI;
Circle.prototype.area = function () {
return this.pi * this.r * this.r;
}

What's not shown in this example is that the prototype can be used to create
an inheritance chain.

If I put this in a file and include it using a script element and my page is
in JScript then the constructor is available in my <% %> blocks but the
prototype assignments are missing (because they haven't been performed yet.)

So I can change to using the SSI style include and change the included file
to be one large <% %> block. This solves the problem for general pages
(unless there's a penalty for having functions in a <% %> block) but now I
can't reuse the same include files from global.asa.

What's the rationale or reason for executing <script> in the page language
and <% %> blocks in separate passes? I can see the issue when mixing
different script languages but when everything is the same language? Why
isn't <script runat="server"> just syntactic sugar for <% %>?

Thanks.


 
Reply With Quote
 
 
 
 
Jason Brown [MSFT]
Guest
Posts: n/a
 
      03-19-2005
the two different script blocks are executed at different times, so it can
cause problems, depending on what it is you're trying to accomplish. I
believe aspfaq.com has an FAQ on this subject which explains it pretty
clearly.

I'm really not sure why it was done this way (the original ASP spec is
pretty old now) but i think it's possible that very early ASP engine code
used one or the other of the conventions and the other was "tacked on"
later. It's an interesting question though, and I'll see what I can find out
on the subject.


--
Jason Brown
Microsoft GTSC, IIS

This posting is provided "AS IS" with no warranties, and confers no rights.


"Jonathan Dodds" <NO_REPLY> wrote in message
news:uvqLTZ%(E-Mail Removed)...
> In "VBScript and JScript Don't Mix, at least in ASP"
> <http://blogs.msdn.com/ericlippert/contact.aspx> Eric Lippert wrote:
>
> Ideally you want the server side <SCRIPT> blocks to contain only global
> function definitions, and the <% %> blocks to contain only "inline" code.
>
>
> Why? Is this a convention or is there a difference between <script>
> element
> blocks and <% %> blocks that make the former better for functions and the
> latter better for inline?
>
> I'm trying to use object-oriented JavaScript in my ASP application. I have
> a
> constructor function and some prototype definitions. Here's an example
> from
> the JScript documentation:
>
> // circle.inc
>
> function Circle (xPoint, yPoint, radius) {
> this.x = xPoint; // The x component of the center of the circle.
> this.y = yPoint; // The y component of the center of the circle.
> this.r = radius; // The radius of the circle.
> }
> Circle.prototype.pi = Math.PI;
> Circle.prototype.area = function () {
> return this.pi * this.r * this.r;
> }
>
> What's not shown in this example is that the prototype can be used to
> create
> an inheritance chain.
>
> If I put this in a file and include it using a script element and my page
> is
> in JScript then the constructor is available in my <% %> blocks but the
> prototype assignments are missing (because they haven't been performed
> yet.)
>
> So I can change to using the SSI style include and change the included
> file
> to be one large <% %> block. This solves the problem for general pages
> (unless there's a penalty for having functions in a <% %> block) but now I
> can't reuse the same include files from global.asa.
>
> What's the rationale or reason for executing <script> in the page language
> and <% %> blocks in separate passes? I can see the issue when mixing
> different script languages but when everything is the same language? Why
> isn't <script runat="server"> just syntactic sugar for <% %>?
>
> Thanks.
>
>



 
Reply With Quote
 
 
 
 
Jonathan Dodds
Guest
Posts: n/a
 
      03-20-2005
My work-around solution is to create factory functions that wrap both the
real constrctors and the prototype statements.

e.g.

// circle.inc

function makeCircle(xPoint, yPoint, radius)
{

function Circle (xPoint, yPoint, radius) {
this.x = xPoint; // The x component of the center of the circle.
this.y = yPoint; // The y component of the center of the circle.
this.r = radius; // The radius of the circle.
}
Circle.prototype.pi = Math.PI;
Circle.prototype.area = function () {
return this.pi * this.r * this.r;
}

var obj = new Circle(xPoint, yPoint, radius);
return obj;
}


"Jason Brown [MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> the two different script blocks are executed at different times, so it can
> cause problems, depending on what it is you're trying to accomplish. I
> believe aspfaq.com has an FAQ on this subject which explains it pretty
> clearly.
>
> I'm really not sure why it was done this way (the original ASP spec is
> pretty old now) but i think it's possible that very early ASP engine code
> used one or the other of the conventions and the other was "tacked on"
> later. It's an interesting question though, and I'll see what I can find

out
> on the subject.
>
>
> --
> Jason Brown
> Microsoft GTSC, IIS
>
> This posting is provided "AS IS" with no warranties, and confers no

rights.
>
>
> "Jonathan Dodds" <NO_REPLY> wrote in message
> news:uvqLTZ%(E-Mail Removed)...
> > In "VBScript and JScript Don't Mix, at least in ASP"
> > <http://blogs.msdn.com/ericlippert/contact.aspx> Eric Lippert wrote:
> >
> > Ideally you want the server side <SCRIPT> blocks to contain only global
> > function definitions, and the <% %> blocks to contain only "inline"

code.
> >
> >
> > Why? Is this a convention or is there a difference between <script>
> > element
> > blocks and <% %> blocks that make the former better for functions and

the
> > latter better for inline?
> >
> > I'm trying to use object-oriented JavaScript in my ASP application. I

have
> > a
> > constructor function and some prototype definitions. Here's an example
> > from
> > the JScript documentation:
> >
> > // circle.inc
> >
> > function Circle (xPoint, yPoint, radius) {
> > this.x = xPoint; // The x component of the center of the circle.
> > this.y = yPoint; // The y component of the center of the circle.
> > this.r = radius; // The radius of the circle.
> > }
> > Circle.prototype.pi = Math.PI;
> > Circle.prototype.area = function () {
> > return this.pi * this.r * this.r;
> > }
> >
> > What's not shown in this example is that the prototype can be used to
> > create
> > an inheritance chain.
> >
> > If I put this in a file and include it using a script element and my

page
> > is
> > in JScript then the constructor is available in my <% %> blocks but the
> > prototype assignments are missing (because they haven't been performed
> > yet.)
> >
> > So I can change to using the SSI style include and change the included
> > file
> > to be one large <% %> block. This solves the problem for general pages
> > (unless there's a penalty for having functions in a <% %> block) but now

I
> > can't reuse the same include files from global.asa.
> >
> > What's the rationale or reason for executing <script> in the page

language
> > and <% %> blocks in separate passes? I can see the issue when mixing
> > different script languages but when everything is the same language? Why
> > isn't <script runat="server"> just syntactic sugar for <% %>?
> >
> > Thanks.
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JavaScript or not JavaScript Mark Rae ASP .Net 36 09-09-2006 01:12 PM
href="javascript:func()" vs href="#" onclick="javascript:func()" CRON HTML 24 06-20-2006 08:05 PM
Javascript -> Applet: variable reset after method call from javascript?! mcdeveloper Java 1 06-13-2006 08:34 AM
asp.net - javascript form values interaction can asp.net read and write to nonasp forms? David Virgil Hobbs ASP .Net 0 01-06-2004 07:09 AM
ASP JavaScript versus ASP VBScript John Davis ASP General 5 09-07-2003 12:42 AM



Advertisments