Home All Groups Group Topic Archive Search About


Author
18 Mar 2005 6:16 PM
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.

Author
19 Mar 2005 3:09 AM
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.


--
Jason Brown
Microsoft GTSC, IIS

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


Show quote
"Jonathan Dodds" <NO_REPLY> wrote in message
news:uvqLTZ%23KFHA.2796@tk2msftngp13.phx.gbl...
> 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.
>
>
Author
20 Mar 2005 4:51 PM
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;
}


Show quote
"Jason Brown [MSFT]" <i-brj***@online.microsoft.com> wrote in message
news:eJO8NEDLFHA.656@TK2MSFTNGP14.phx.gbl...
> 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%23KFHA.2796@tk2msftngp13.phx.gbl...
> > 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.
> >
> >
>
>

AddThis Social Bookmark Button