JavaScript Introduction

 

Introduction

JavaScript was developed by Netscape as a means to bring the simplicity of scripting languages to a wider audience. The main difference between Java and JavaScript, is that Java code is compiled into "applets" that are distinct from the HTML page and whose code cannot be viewed directly, whereas JavaScript code is included directly in the HTML document and can be easily modified.

JavaScript is a scripting language designed to extend the functionality of web pages. Typically written as part of an HTML document, a JavaScript controls the elements of the page and reacts to user actions.

The generic JavaScript language is distinguished from the javaScript language by an uppercase letter "J". The javaScript language is a proprietary javaScript compiler generated by parseLib. The javaScript compiler Lambda converts javaScript source code into an Analytic Information Server parse tree which is the input to the morph and compile functions. The compile function finally produces an Lambda ready execute.

Thus, the javaScript language is an easy to use alternative tool that web programmers may find more easy to use than Lisp. The javaScript language extends the power of Lambda Oriented programming to a wider audience.

javaScript includes a number of language commands for creating datamine tables, filtering tables, and scoring tables. It was developed, for the dataMineLib, as a means to bring the full power and simplicity of scripting languages to the data mining process.

Thus, the Filter Extensions is an easy to use alternative tool that dataMineLib programmers may find more easy to use than the data mine Lambda?s API. These language extensions bring the power of scripting languages to the data mining process.

Invoking javaScript from Lisp

The javaScript compiler Lambda can be called from the Lisp parser, lisp, by using the #javaScript# compiler directive, or it can be called directly. If called directly, it should be followed by morph and compile (see the lisp parser - javaScript is a replacement for the lisp parser). The javaScript Lambda was generated by the parseLib Lambda.

Note: The javaScript Lambda must be loaded into the workspace prior to using the javaScript language. (see the javaScript library and the Dynamic HTML example).

Invoking javaScript from the Console

The LambdaClient IDE contains a Console tab. Clicking the right mouse button, and selecting the "Set Prefix" option displays a modal dialog containing an edit box for the user to enter a Console prefix. The Console prefix controls which parser is invoked when the console receives any text to be evaluated. The valid prefixes are: "" (a null string), "#lisp#", and "#javaScript#". If the Console prefix is a null string or #lisp#, then any expressions submitted for evaluation will be parsed with the Lisp parser. If the Console prefix is "#javaScript#", the JavaScript parser will be invoked.

Lambda Objects

The LambdaClient development uses an Lambda-oriented metaphor. javaScript "Lambdas" are a unique class of objects designed to act as the building blocks for intelligent, adaptive, systems. Lambdas contain more than just binary machine code (Analytic Information Server supports many built-in functions, which are primarily binary machine code, but these are not Lambdas). Lambdas are something more than just functions. Lambdas are building block objects, which contain the necessary structure to provide some rudimentary autonomy. Lambdas can contain other child Lambdas and can give birth to other child Lambdas. Lambdas can publish their preferred style of interface. Lambdas have an abstract threshold (like a cell membrane) which makes the Lambda aware of any mutative or referential access attempt from the outside. Lambdas may run on native machine code or they may be emulated by a virtual machine. There may be a different virtual machine for each Lambda. Lambdas contain their persistent and temporary knowledge variables. Lambdas contain the original source code used to compile them. Lambdas can be generated from multiple languages. Analytic Information Server comes with a built-in Lisp compiler, which produces Lambdas.

Lambda Properties

The Lambda object stores Lambda behavior and knowledge in a standard building block format (regardless of the original source language). The Analytic Information Server Lambda object contains the following eight properties:

Av:

The arguments Structure object containing the Lambda's arguments.



In:

The faces: Structure object containing the Lambda's published interface styles.

Pc:

The Pcode Vector object containing the Lambda's virtual machine codes.

Pv:

The pvars: Structure object containing the Lambda's persistent variables.

Cv:

The cvars: Structure object containing the Lambda's persistent class variables

Nc:

The Native Code Vector object containing the Lambda's native machine code.

Sc:

The Source Code Vector containing the original language source for debugger display.

Tv:

The vars: Structure object containing the Lambda's temporary frame variables.

Vm:

The Virtual Machine emulator function (each Lambda may run on a separate virtual machine).

 

An Lambda is First Class Object. A First Class object in Lambda Information Server is any object that is fully exposed, i.e., all of the Structures are visible and modifiable by the programmer. All Lambdas have the following data structures: source code tokens (Sc), pcode tokens (Pc), argument variables (Av), persistent variables (Pv), persistent class variables (Cv), temporary variables (Tv), interfaces (In), native code (Nc), and the virtual machine emulator (Vm). All Lambda structures can viewed and modified by the programmer:

javaScript Filter Lambdas

The principal activity of the datamine is to reduce tables to smaller tables which have a higher score than the original table. This process is called filtering. For instance, in the stock market, we start with a table of all possible stocks and we wish to select a few stocks to purchase. If the few stocks we purchase have a higher score (percent profit) than the average of all stocks, then we are happy. The act of selecting a few stocks to purchase reduces the original table of all stocks down to the table of those we wish to purchase. This process is called filtering.

Other examples of filtering include reviewing a table of all United States households to select only those households which are to receive this month's promotional mailing. Reviewing a table of possible oil deposit sites to select only those sites where we wish to drill. There are many other examples of filtering.

The score of a table is determined by the Score Lambda (see the Score Lambdas section next).

All datamine table filtering is performed by Filter Lambdas, which are entered by the user in the simple, easy to learn datamine javaFilter language. An example might be:

filter top LastMonthProfit 10%;

or

filter all Timeliness = 1;

javaScript Score Lambdas

If the principal activity of the datamine is to reduce tables to smaller tables which have a higher score than the original table, then there must be some means of determining a table?s score. All datamine table scoring is performed by Score Lambdas, which are entered by the user in the simple, easy to learn datamine javaFilter language. An example might be:

score avg NextMonthProfit;

or

score sum ItemPrice * TotalItems;

JavaScript Data Types

The javaScript Programmer has access to all of the types in the Analytic Information Server environment. These are the same as the Lisp data types. The Lisp data types are divided into three categories: Native Data Types (also known as Immediate types), Objects (heap objects) and Repositories. The Native (immediate) types can be entirely contained within the immediate data of a single Virtual Machine Container. The Objects (heap objects) types are too large to be contained within a single Virtual Machine Container and require extra memory must be managed by the heap manager. Without exception, all of the Object types are identified by an object id. The object id identifies a block of memory, managed by the Analytic Information Server memory manager, in which the Object's data is stored. (see Object Identifier Notation).

Virtual Machine Containers are of fixed length and come in different sizes. Small data items are stored in immediate mode, and may be moved to the heap if the data becomes too large to store immediately.

The Heap contains memory resident data, which is of variable length or is too large to fit in small fixed containers. The Analytic Information Server object Heap manager supports automated object resizing, garbage collection, and anti-fragmentation algorithms so that the user may concentrate on the analysis and modeling of data rather than on memory management.

Repositories (databases) contain persistent data of all sorts. Lambda Information Server supports repositories with multiple database volumes and multiple database schema's including General Object Repositories, Text Repositories, and Lambda Repositories.

The generic Analytic Information Server data type is known to javaScript as obj. No type identification, such as var n;, will cause javaScript to treat the variable, n, as being of type obj, that is to say any possible Analytic Information Server data type.

The javaScript compiler also supports strong typing of declared variables obj. Providing a type identification, such as var int n;, will cause javaScript to treat the variable, n, as being of type int, that is to say it will be managed as an Analytic Information Server type Integer.

The following is a list of javaScript strong data types together with the Analytic Information Server types which they represent.

obj Object bool Boolean char Character int Integer float Number
text Text string String symbol Symbol bytvec ByteVector fltvec FloatVector
stc Structure dir Directory dic Dictionary matrix Matrix nummat NumMatrix
vec Vector bitvec BitVector numvec NumVector intvec IntVector objvec ObjVector

Strong Typing

The javaScript Programmer has access to compile time strong typing variable declarations. Strongly typed variables are compiled with Lambda Information Server's strong typed virtual machine instructions. Strongly typed variables operate faster, at run time; but, are more prone to programmer error as there is little or no run time type checking performed.

The programmer can even cast an arbitrary javaScript expression to a valid type. The casting will alert the javaScript compiler to treat the result of the cast expression as specified. This will direct the javaScript compiler to use Analytic Information Server's strong typed virtual machine instructions with the cast expression. Warning: casting does not introduce any run time type checking.

The following javaScript code sample illustrates the actions of the javaScript compiler when strong typing variable declarations and type casts are encounteres.

The javaScript source code for foo

              // A test of strong typing, including expression type casting, in javaScript.
              function foo(int i) {
              var char c1, string name=new('String',"Hello There");
              c1 +=name[((int)length(name))-i];
              }

The compiled code for foo

Virtual Machine Instructions for: foo
0000: push "String,"Hello There"
0007: call 2,new,vars:(name)
0011: push vars:(name)
0013: call 1,length,vars:(__T4)
0017: isub args:(i),vars:(__T4),vars:(__T3)
0021: refstring vars:(__T3),vars:(name),vars:(__T2)
0025: cadd vars:(__T2),vars:(c1),vars:(c1)
0029: return vars:(c1)

White Space

The javaScript compiler uses white space to separate each of its symbols and operators. The javaScript white space characters include all the standard 8-bit ASCII control characters (less than 32 decimal), and the blank character (32 decimal).

LF, CR, TAB ..control chars..

space

The javaScript compiler ignores whitespace

a = 1 + 2;  // This is a valid statement
               b=1+2;  // This is also a valid statement

Special Characters

javaScript uses the standard 8-bit ASCII character set. Some of the javaScript special characters serve to group a set of characters as a single unit (e.g. double quotes group characters to form a string constant). The remainder of the special characters serve to separate tokens (e.g. comma or blank) or prefix a constant (e.g. $ # ).

The following are the javaScript special characters.

\ | ( ) [ ] { } # @
' ' , " : ; $ % .  

Naming Conventions for Variables

javaScript variable names are composed of case-sensitive alphanumeric characters. No spaces are allowed in a variable name but the underscore (_) character may be embedded to separate multi-word names . Another convention to make multiple word names more readable its to use start the first word with a lowercase letter and begin the first letter of each succeeding word with an uppercase letter.

For example

myVariable
        
        sum
        
        namesOfStudents

Constants

javaScript is a dynamically typed language. The type of a variable is unknown until runtime when data is stored into it. The follow table contains the constant forms recognized by the javaScript compiler. For more detail on the data types listed below, see Analytic Information Server Programmer's Guide.

Type Constant Form
Void void or nil
Boolean true or false
Date #Mar,2,1987 or #Jun,1,200BC
Integer 12 or -2345
Number 12.9 or 0.123456
Object #<Vector 1273>
String "Hello World"
Symbol 'Hello'

Comments

Because the javaScript compiler tries to evaluate all of the words in a script, it is useful to have text, which is to be ignored by the compiler. This ignored text, called a comment, allows you to include information, which may be useful to understanding the javaScript statements. There are two types of comments: single line and multi-line.

A single line comment tells the compiler to ignore all the characters up to the end-of-line (eol). A single line comment must begin with the characters: //

For Example:

// This is a comment

A multi-line comment tells the compiler to ignore all the characters embedded in between the delimiters: /* and */

For Example:

/*  Humpty Dumpty 
        
                    sat on a wall  */

Global Variables

javaScript variables have automatic global declaration. Referencing a symbol, which has not already been declared, automatically causes it to be declared as a global variable. This feature has been added to make javaScript user-friendlier and to make javaScript consistent with other Analytic Information Server languages.

The following javaScript expressions are equivalent (The assumption is made that X has not already been referenced).

X = 23

is equivalent to:

var X = 23

javaScript global variables are valid during the whole life of the current workspace (see the _globals global symbol table variable). javaScript global variables are referenced by specifying the symbol. In addition to user defined globals, javaScript global variables include all of the built-in functions such as + - * upperCase, sin, cos, date, etc.

The Analytic Information Server javaScript dialect is specified as case-sensitive (most dialects of javaScript are case-insensitive). Therefore

Var

is NOT equivalent to:

var

Function Calls

Any user-defined javaScript Lambda, and Lisp Lambda, and any Lambda Information Server function may be called from javaScript. The syntax is simple the function name followed by parenthesis, (). If the function requires arguments, they must be supplied in between the parenthesis and multiple arguments should be separated by a comma. The parenthesis are mandatory even if no arguments are supplied. All javaScript functions receive arguments by value. After function invocation, one and only one result value is returned.

For example

mod(10, 2);         //Returns 0
        
                    today();            //Returns 729855