OO javascript in ASP

J

Jonathan Dodds

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.
 
J

Jason Brown [MSFT]

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.
 
J

Jonathan Dodds

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top