OO javascript in ASP

Discussion in 'ASP General' started by Jonathan Dodds, Mar 18, 2005.

  1. 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.
     
    Jonathan Dodds, Mar 18, 2005
    #1
    1. Advertising

  2. 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%...
    > 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.
    >
    >
     
    Jason Brown [MSFT], Mar 19, 2005
    #2
    1. Advertising

  3. 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]" <> wrote in message
    news:...
    > 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%...
    > > 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.
    > >
    > >

    >
    >
     
    Jonathan Dodds, Mar 20, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Virgil Hobbs
    Replies:
    0
    Views:
    533
    David Virgil Hobbs
    Jan 6, 2004
  2. mcdeveloper
    Replies:
    1
    Views:
    4,229
    mcdeveloper
    Jun 13, 2006
  3. CRON
    Replies:
    24
    Views:
    201,790
    Adrienne Boswell
    Jun 20, 2006
  4. Mark Rae

    JavaScript or not JavaScript

    Mark Rae, Sep 5, 2006, in forum: ASP .Net
    Replies:
    36
    Views:
    1,112
    Paul Sture
    Sep 9, 2006
  5. John Davis

    ASP JavaScript versus ASP VBScript

    John Davis, Sep 6, 2003, in forum: ASP General
    Replies:
    5
    Views:
    146
    Curt_C [MVP]
    Sep 7, 2003
Loading...

Share This Page