Home

JScript .NET Fundamentals

 

Overview of JScrip .NET

 

Introduction

JScript .NET is a computer programming language developed by Microsoft based on early or previous standards of the JavaScript, then JScript language. The primary and strongest difference, what sets JScript .NET apart is that it is not a simple or regular scripting language. JScript .NET is actually at the same level of, and enjoys the same respect of popular languages like Java, C/C++, or C#. While the previous version of JScript and JavaScript were scripting languages that needed a browser to show their results, JScript .NET is a fully functional and independent language that can be used to create regular executables that can be run on client computers.

JScript .NET can be as loose as JavaScript, that is, it can let you write code that don't rely on known data types. At the same time, you can take advantage of techniques used in strongly typed languages like C/C++, Pascal, C#, etc, making the language "less forgiving". This makes the code easier to read, maintain, and faster to compile.

Types of Applications

The acronym .NET suggests that JScript .NET uses the .NET Framework to take full advantage of the operating system on which it is run. This means that you can create as much a complete and fully functional application using JScript .NET the same way you would with Visual Basic or Visual C#. This also means that, using JScript .NET, you can use the same libraries, the same classes, and the same functions of Visual Basic, Visual C#, and other languages of the .NET Framework family.

With JScript .NET, you can create console applications, graphical (GUI) Windows application, web-based applications (ASP.NET), and many more. This is because well written code in a JScript .NET has direct access to the .NET Framework. A JScript .NET application also enjoys support of the Common Language Specification (CLS), the bottom language standard that other .NET Framework languages follow. This specification provides the use of data types and the interoperability that languages need in order to "exchange" code. This means that code written in JScript .NET can use classes, libraries, and other objects including the contents of namespaces, created from the other CLS compliant languages even if the JScript .NET programmer doesn't know those languages.

Applications Fundamentals

 

The Compiler

To create a JScript .NET application, you start by writing the necessary code. Code for a JScript .NET application is create as normal text in a regular computer file. After writing the necessary code, you save the file in a directory of your choice. The file must have the extension .js.

Code of a JScript .NET application is written in plain English with some words you are familiar with and some other words you will encounter as you move. If you write a regular JScript script, you can open it in a browser or a dialog box (for a Windows Script Host application). If you are creating a console or a graphical user interface (GUI) application that would result in an execute that can be used on other languages without caring for the browser, then you must "translate" your code in a language the computer can understand. As mentioned already, your code is written in English. Unfortunately, the computer doesn't understand it.

To perform this "translation", you must use another program that understands both languages. This program is called a compiler. Fortunately, Microsoft provides a free compiler you can use. The compiler provided by Microsoft for JScript .NET applications is called jsc.exe. To get it, you must download the Microsoft .NET Framework from the Microsoft web site. If you are using Microsoft Windows XP or 2003, the library should be installed already on your computer. If you are using earlier versions of Microsoft Windows, you make need to check or download it, free, from Microsoft. After downloading and installing the library, you are ready to use it.

After installing the .NET Framework, you can create an application in the same directory where the jsc application is installed. If you create a JScript .NET file and save it in another directory, when you attempt to compile your application, you may receive an error. If you want to be able to create a JScript .NET application from any folder of your choice, you can modify the Autoexec.bat or the Environment Path of your application.

 

Practical LearningPractical Learning: Adding the Compiler to the Path

  1. Start Windows Explorer and locate the folder where your jsc application is installed. Here is an example:
     
  2. Click Start -> Control Panel
  3. Double-click System
  4. In the System Properties dialog box, click the Advanced tab and click Environment Variables
  5. In the System Variables section, click Path and click Edit
  6. Press End or get to the end of the string
  7. Type ; followed by the complete path to the compiler. Mine appears as:
     
    %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322
  8. Click OK on each dialog box

Application Creation

To create a JScript .NET application, you start with a text file and write your code in it. One of the types of applications you can create is called a console application. A console application is one that displays its result(s) in a DOS window, which is a window with a black background.

In our lessons, we will mostly use the free jsc compiler provided by Microsoft. To write our code, we will use Notepad. To compile our applications, we will use the command prompt.

There are two main operations you will regularly perform as a programmer vis-à-vis your application. The primary operation consists of displaying something on the screen. This task is taken care of by using print(). Based on this, to display a word or sentence on the DOS window, include it in the parentheses of print. If it is a word or a group of words, include it between double-quotes.

 

Practical LearningPractical Learning: Introducing JScript .NET Code

  1. Start Notepad and in it, type print("Welcome to the Wonderful World of JScript .NET");
  2. To save the file, on the main menu, click File -> Save
  3. Select and display the C: drive in the Save In combo box
  4. Click the Create New Folder button Create New Folder
  5. Type JScript Lessons
  6. Press Enter twice or display the new folder in the Save In combo box
  7. Click the Create New Folder button Create New Folder again
  8. Type Exercise1 and press Enter twice or display the new folder in the Save In combo box
  9. Save the file as exercise.js
     
    Save As
  10. Click Save

Application Compilation

After creating a file that holds the code of a JScript .NET application, you can prepare it for distribution. A regular script is a simple text-base file that can be interpreted by a browser. If you create such a script, you can save it to display and make it available to other people who would simply open it in their browser.

If you have created a JScript .NET application and want to distribute but you are not concerned whether your clients have a browser or not, you must create an executable. To do this, you must compile your application. The only way you can create an executable from a JScript .NET application is through the command prompt. The steps used to open the command prompt depend on your operating system. In Windows XP and Windows Server 2003, to open a command prompt, on the Taskbar, you can click Start -> All Programs -> Accessories -> Command Prompt.

To compile a program on the Command Prompt, first change to the directory that contains your script file. To proceed, invoke the name of your compiler, in this case jsc, followed by the name of the script file wit its extension, and press Enter. If there are errors, the compiler will let you know. You can then return to the file, fix the problem, and compile again, until all problems have been corrected.

After compiling the program, an executable application, that has the same name as the script that was compiled, would be created.

Practical LearningPractical Learning: Compiling an Application

  1. To test the application, open the Command Prompt and change to the folder in which you created the JScript .NET file:
     
    DOS Window
  2. Type jsc exercise.js and press Enter

Application Execution

After creating an executable application, you can distribute it to other people. An executable of a JScript .NET application is a fairly small application. You can save it to disc and then, either place if where people can first download it or send it in a disk or other portable medium (never email an executable).

Executing your application is equivalent to using it. To execute an application that you have just created, at the Command Prompt where you compiled it, you can type the name of the executable (your clients can double click the executable to open the application.

Practical LearningPractical Learning: Executing an Application

  1. When the program has finished compiling, type exercise and press Enter
     
    DOS Window
  2. Type Exit to close the DOS window

A Custom Name for an Executable

As mentioned above, if you compile your application by simply invoking the name of the script file that contains the code, an executable with the same name as the file would be created. Fortunately, when compiling the file, you can specify the name you want for the executable. The formula to follow is:

jsc /out:ApplicationName File.js

In this formula, you must first call the compiler as done previously. It must be followed by /out: which indicates that you are going to specify the name of the executable. After the /out: factor, type the desired name that will hold the name of the new application instead of the name of the file. Lastly, type the name of the file that contains the script and make sure you append its extension.

 

Custom Libraries

A library is a program that contains additional information that other programs can use. The information in the library can be almost anything. Experience will guide you as to what you can put in it.

A library is created with the same approach as the programs we have used so far. This means that you start from a script file that has the .js extension and that contains the code you decided to put in it. To actually create the library, you compile it using the following formula:

jsc /target:library file.js

In this case, the only thing different in the formula is the file.js, which is the name of the file that contains the necessary code. After compiling the code, a new application (yes, it is an application) is created but it has the .dll extension instead of .exe as do the regular applications we have created so far.

Accessories for Coding

 

Comments

A comment is a line or paragraph of text that the compiler would not consider when examining the code of a program. There are two types of comments recognized by C#.

To display a comment on a line of text, start the line with two forward slashes //. Anything on the right side of // would be ignored. Here is an example:

// This line will be ignored. I can write in it anything I want

The above type of comment is used on only one line. You can also start a comment with /*. This type of comment ends with */. Anything between this combination of /* and */ would not be read by the compiler. Therefore, you can use this technique to span a comment on more than one line.

Escape Sequences

An escape sequence is a special character that displays non-visibly. For example, you can use this type of character to indicate the end of line, that is, to ask the program to continue on the next line. An escape sequence is represented by a backslash character, \, followed by another character or symbol. For example, the escape sequence that moves to the next line is \n.

An escape can be included in single-quotes as in '\n'. It can also be provided in double-quotes as "\n".

JScript.NET recognizes other escape sequences.

Escape Sequence Name Description
\b Backspace Takes the cursor back by one space
\t Horizontal Tab Takes the cursor to the next tab stop
\n New line Takes the cursor to the beginning of the next line
\v Vertical Tab Performs a vertical tab
\f Form feed  
\r Carriage return Causes a carriage return
\s Any Character Matches any white space character
\S Any Character Matches any non-white space character
\' Single Quote Displays a single quote
\" Double Quote Displays a double quote
\\ Backslash Displays a backslash (\)
\h Character Displays an ASCII character from an octal number
\xhh 2-Digit Hex Displays a hexadecimal number with two digits
\xhhhh 4-Digit Hex Displays a hexadecimal number with four digits
 

Copyright © 2004-2005 FunctionX, Inc. Next