JScript .NET Fundamentals
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.
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 Learning: Adding the Compiler to the Path
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.
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.
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.
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.
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.
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.
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.
|Takes the cursor back by one space
|Takes the cursor to the next tab stop
|Takes the cursor to the beginning of the next line
|Performs a vertical tab
|Causes a carriage return
|Matches any white space character
|Matches any non-white space character
|Displays a single quote
|Displays a double quote
|Displays a backslash (\)
|Displays an ASCII character from an octal number
|Displays a hexadecimal number with two digits
|Displays a hexadecimal number with four digits
|Copyright © 2004-2005 FunctionX, Inc.