To read the article online, visit http://www.4GuysFromRolla.com/webtech/061800-1.2.shtml

Server-Side JScript Objects, Part 2

By Richard Lowe


  • Read Part 1

  • In Part 1 we looked at the createTicket() function for our Ticket object. In this part we will look at how to tie the createTicket() function into the Ticket object by using an object constructor.

    The Object Constructor
    The next step is to define the JavaScript object with an object constructor. An object constructor is a simply a JavaScript function with a specific syntax that is used to define the elements of an object. It looks like this:

    // Object Constructor Syntax
    // When you create a new object in JScript, parameters can be passed
    // to it just like the intrinsic JavaScript objects
    function MyObjectConstructorName(anyParametersToPassWhenCreating)
    {
    // non-functions become properties;
      this.OnePropertyName;
    
    // var type can be set explicitly;  
      this.AnotherPropertyName = new String();
    
    // functions referenced by this. become methods;
      this.AFunctionName;
    }
    
    // This object is 'created' in this way:
    var objVariable = new MyObjectConstructorName('SomeParameter');
    

    This syntax can then be applied to the ticket object for the helpdesk project:

    // The cTicket function is the object constructor 
    // which defines our JavaScript object
    function cTicket()
    {
      this.user = new String();
      this.description = new String();
      this.severity = new Number();
    	
    // This assigns the function to the create method
      this.create = createTicket;
    }
    

    The object is now available to ASP using JScript, however most developers will want to continue using VBScript as their primary ASP language. This creates another step in the process, returning an object to VBScript with one more small function:

    function Ticket()
    {
    // Creates an instance of the object defined by cTicket
      var obj = new cTicket();
    	
    // And returns it to the function (which is available to VBScript)
      return(obj);
      obj = null;
    }
    

    The partially completed ticket object is now ready to be used in VBScript. It is easiest to place objects into #include files on their own, so that the includes on a particular page are as finely granular as possible.

    <% @language='VBScript' %>
    <!--#include file="objTicket.asp"-->
    <%
      On Error Resume Next
    
    ' Dim the variable for the new JScript Ticket object
      Dim objTicket
      
    ' Set the objTicket reference to the result of the Ticket function  
      Set objTicket = Ticket()
    
    ' It's now possible to reference all the properties and methods
    ' of this object instance
      With objTicket
        .user = "Richard Lowe"
        .description = "Can't boot his PC"
        .severity = 3
      End With
    
      ' Call the create method above and return it's result
      Response.Write("The error code is " & objTicket.create())
    
      Set objTicket = Nothing
    	
      ' This is here in case a severe error is returned!
      If Err.Number <> 0 Then
        Response.Write("<br>Oops!: " & Err.Number & _
                       "<br>" & Err.Description)
      End If
    %>
    

    Note, the above script uses with With block, requiring VBScript 5.0 or greater. To learn more about the With block be sure to read: Using the With Block. To determine your VBScript version be sure to read: Determining the Server-Side Scripting Language and Version.

    Implementing JScript objects in a VBScript-driven ASP page takes some planning and patience. However a properly implemented set of JScript objects in finely granular #include files can significantly reduce the cycle time in most sizes of ASP-based project because they:

    • Provide functionality that can be re-used by any developer who's ever used COM objects in ASP.
    • Allows script-only programmers to use object design prinicples in their ASP application.
    • Place functions and properties into logical, easy to use groupings.

    Happy Programming!

    Richard Lowe (chadich@yahoo.com)
    Richard Lowe works as a Development Services consultant for Spherion in the Technology Group


    Attachments:

  • Download the source for objTicket.asp in text format


  • Article Information
    Article Title: Server-Side JScript Objects, Part 2
    Article Author: Richard Lowe
    Published Date: Sunday, June 18, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/061800-1.2.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers