JScript

From Bauman National Library
This page was last modified on 1 June 2016, at 17:12.
JScript
Developer Microsoft
First appeared 1996
Stable release 9.0 / March 2011
Typing discipline Dynamic, weak, duck
OS Microsoft Windows
Filename extensions .js, .jse, .wsf, .wsc (.htm, .html, .asp)
Website Template:Url
Major implementations
Active Scripting, JScript .NET

JScript is Microsoft's dialect of the ECMAScript standard that is used in Microsoft's Internet Explorer. JScript is implemented as an Active Scripting engine. This means that it can be "plugged in" to OLE Automation applications that support Active Scripting, such as Internet Explorer or other. It also means such applications can use multiple Active Scripting languages, e.g., JScript or PerlScript. JScript was first supported in the Internet Explorer#Internet Explorer 3|Internet Explorer 3.0 browser released in August 1996. JScript 10.0 is a separate dialect, also known as JScript .NET, which adds several new features from the abandoned fourth edition of the ECMAScript standard.

Features

JScript Arrays

An Array object is a variable that groups related pieces of data. A unique number, called an index or subscript, references each piece of data in the array. To access the data stored in the array, the array identifier and the index are combined with the index operator "[]", for example, theMonths[0].

var theMonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

Using Expando Properties of Arrays

Array objects, just as any other object based on the JScript Object object, support expando properties. Expando properties are new properties that you dynamically add and delete from an array, like array indices. Unlike array indices, which must be whole numbers, expando properties are strings. In addition, adding or deleting expando properties does not change the length property.

// Initialize an array with three elements.
var myArray = new Array("Hello", 42, new Date(2000,1,1));
print(myArray.length); // Prints 3.
// Add some expando properties. They will not change the length.
myArray.expando = "JScript";
myArray["another Expando"] = "Windows";
print(myArray.length); // Still prints 3.

Typed Arrays

Another faster way to create the theMonths array shown above is to create a typed (native) array, in this case, an array of strings:

var theMonths : String[] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

JScript Assignments and Equality

In JScript, the assignment operator assigns a value to a variable. An equality operator compares two values.

Assignments

Like many programming languages, JScript uses the equal sign (=) to assign values to variables: it is the assignment operator. The left operand of the = operator must be an Lvalue, which means that it must be a variable, array element, or object property.

The right operand of the = operator must be an Rvalue. An Rvalue can be an arbitrary value of any type, including a value that is the result of an expression. Following is an example of a JScript assignment statement.

var i : int = 29;
var x : double = i;

Equality

Unlike some other programming languages, JScript does not use the equal sign as a comparison operator, but only as the assignment operator. For comparison between two values, you can use either the equality operator (==) or the strict equality operator (===). Comparisons in your scripts always have a Boolean outcome. Consider the following line of JScript code.

y = (x == 2000);

JScript Comments

A single-line JScript comment begins with a pair of forward slashes (//).

// This is a single-line comment.
aGoodIdea = "Comment your code for clarity.";

A multiline JScript comment begins with a forward slash and asterisk (/*), and ends with the reverse (*/).

/*
This is a multiline comment that explains the preceding code statement.
The statement assigns a value to the aGoodIdea variable. The value, 
which is contained between the quote marks, is called a literal. A 
literal explicitly and directly contains information; it does not 
refer to the information indirectly. The quote marks are not part 
of the literal.
*/

JScript Expressions

A JScript expression is a combination of keywords, operators, variables, and literals that yield a value. An expression can perform a calculation, manipulate data, call a function, test data, or perform other operations. The simplest expressions are literals. Here are some examples of JScript literal expressions. For more information, see Data in JScript.

3.9                               // numeric literal
"Hello!"                          // string literal
false                             // Boolean literal
null                              // literal null value
[1,2,3]                           // Array literal
var o = {x:1, y:2}                // Object literal
var f = function(x){return x*x;}  // function literal

Expressions that are more complicated can contain variables, function calls, and other expressions. You can use operators to combine expressions and create complex expressions. Examples of using operators are:

4 + 5        // additon
x += 1       // addition assignment
10 / 2       // division
a & b        // bitwise AND

JScript Identifiers

In JScript, identifiers are used to:

  • name variables, constants, functions, classes, interfaces, and enumerations
  • provide labels for loops (not used very often).

Here are some examples of valid identifiers:

_pagecount 
Part9 
Number_Items

Here are some examples of invalid identifiers:

99Balloons // Cannot begin with a number. 
Smith&Wesson // The ampersand (&) character is not a valid character for variable names.

JScript Statements

A JScript program is a collection of statements. A JScript statement, which is equivalent to a complete sentence in a natural language, combines expressions that perform one complete task.

var aBird = "Robin"; // Assign the text "Robin" to the variable aBird.
var today = new Date(); // Assign today's date to the variable today.

JScript Data Types

JScript provides 13 primitive data types and 13 reference data types. In addition to these, you can declare new data types or use any of the Common Language Specification (CLS)-compliant .NET Framework data types. This section includes information about the intrinsic data types, how to extend those types, how to define your own data types, how to enter data, and how to convert data from one type to another.[1]

JScript Variables and Constants

Variables and constants store data that scripts can easily reference by using the name of the variable or constant. Data stored by a variable may change as a program runs, while data stored by a constant cannot change. A script that uses a variable actually accesses the data that the variable represents. Here is an example in which the variable named NumberOfDaysLeft is assigned the value derived from the difference between EndDate and TodaysDate.

NumberOfDaysLeft = EndDate – TodaysDate;

JScript Objects

A JScript object is an encapsulation of data and functionality. Objects are composed of properties (values) and methods (functions). Properties are the data component of the object, while methods provide the functionality to manipulate the data or the object. JScript supports five kinds of objects: intrinsic objects, prototype-based objects, class-based objects, host objects (provided by a host, such as Response in ASP.NET) and .NET Framework classes.

The new operator in conjunction with the constructor function for the selected object creates and initializes an instance of an object. Here are a few examples that use constructors.

var myObject = new Object();           // Creates a generic object.
var birthday = new Date(1961, 5, 10);  // Creates a Date object.
var myCar : Car = new Car("Pinto");    // Creates a user-defined object.

JScript supports two types of user-defined objects (class-based and prototype-based). Both types have unique advantages and disadvantages. Prototype-based objects are dynamically extensible, but they are slow and do not interoperate efficiently with objects from other .NET Framework languages. Class-based objects, on the other hand, can extend existing .NET Framework classes, help provide type safety, and help foster efficient operation. Class-based objects can be dynamically extensible (like prototype-based objects) by defining the class with the expando modifier.

JScript Modifiers

Visibility Modifiers

Visibility modifiers restrict how outside code accesses classes, interfaces, and their members. You can use restrictions to encourage good object-oriented programming practices by preventing calls to specialized internal methods and fields.

By default, any code that can access a class can access any of the members of that class. Using the visibility modifiers, you can selectively prevent outside code from accessing particular class members, allow only classes from the same package to access members, or allow only derived classes to access class members.

Visibility modifiers cannot be applied to global functions or variables. The only visibility modifiers that can be used together are protected and internal.

Inheritance Modifiers

Inheritance modifiers control how methods and properties from derived classes override methods and properties in a base class. By using this control, you can manage whether methods from derived classes will override a class you create.

By default, methods from a derived class will override base class methods unless the version-safe hide attribute is used in the derived class. This attribute helps prevent overriding. Using inheritance modifiers enables you to help control whether particular methods are overridden always or never.

In some situations, you may need to ensure that a base class method is not overridden. For example, if you define a class in a package, you can use the final modifier to ensure that derived classes will not change the methods and properties of the class.

On the other hand, you may want to require your class to have certain methods overridden. For example, you can create a class that provides some basic functionality but uses the abstract modifier for some methods. The implementations of the abstract methods are up to the writer of the derived class.

Version-safe modifiers, which also manage overriding, manage it from the derived-class side rather than the base-class side. Version-safe modifiers have an effect only if the base-class method they are overriding does not have inheritance modifiers.

You cannot combine two inheritance modifiers or combine an inheritance modifier with the static modifier.

Version-Safe Modifiers

Version-safe modifiers help control the methods from a derived class that override methods in a base class. By using this control, you can manage whether a class you create will override methods in the base class.

By default, methods from a derived class will override methods in a base class, although inheritance modifiers in the definition of the derived class can help prevent overriding as well. Using version-safe modifiers enables you to help control whether particular methods are overridden or not.

In some situations, you may need to ensure that base-class methods are not overridden. For example, you may extend a class to change the behavior of the base-class methods. If you do not want those methods to be overridden in the base class, you can use the hide modifier for your method declarations.

On the other hand, you may want to override certain base-class methods. For example, you may want to change the methods of a class without modifying the class. By extending the class and using the override modifier for your method declarations, you can cause the new methods to override the base class.

Successful use of version-safe modifiers depends on whether or not the declaration of the base class methods used inheritance modifiers. Base-class methods marked with the final modifier cannot be overridden, and base-class methods marked with the abstract modifier cannot be hidden unless an explicit implementation for the abstract base-class method is given.

You cannot combine two version-safe modifiers or combine a version-safe modifier with the static modifier. When you are running in version-safe mode, you can use only one version-safe modifier for each method that overrides a base-class method.

expando Modifier

The expando modifier causes a class-based object to behave as if it were a JScript object. Methods and properties can be dynamically added to an expando object. For more information, see Prototype-based Objects.

You can use the expando modifier independently of the other modifiers.

static Modifier

The static modifier signifies that a member of a class belongs to the class itself rather than to instances of the class. Consequently, class-specific data and methods may not be associated with any particular instance.

You cannot combine the static modifier with any of the version-safe or inheritance modifiers.

JScript Operators

JScript has a full range of operators, including computational, logical, bitwise, and assignment, as well as some miscellaneous operators. Operators combine simple expressions to compose more complex expressions.

JScript Functions

JScript functions can perform actions, return values, or both. For example, a function could display the current time and return a string that represents the time. Functions are also called global methods.

Functions combine several operations under one name, which makes code streamlined and reusable. You can write a set of statements, name it, and then execute the entire set by calling its name and passing to it the necessary information.

To pass information to a function, enclose the information in parentheses after the name of the function. Pieces of information that are passed to a function are called arguments or parameters. Some functions do not take arguments, while others take one or more arguments. In some functions, the number of arguments depends on how you are using the function.

JScript supports two kinds of functions, those that are built into the language and those that you create.

Coercion in JScript

JScript can perform operations on values of different types without the compiler raising an exception. Instead, the JScript compiler automatically changes (coerces) one of the data types to that of the other before performing the operation. Other languages have much stricter rules governing coercion.

var x = 2000;      // A number.
var y = "Hello";   // A string.
x = x + y;         // the number is coerced into a string.
print(x);          // Outputs 2000Hello.

Copying, Passing, and Comparing Data

By Value vs. By Reference

JScript copies, passes, and compares numbers and Boolean values (true and false) by value. This process allocates a space in computer memory and copies the value of the original into it. Changes to the original do not affect the copy (and vice versa) because the two are separate entities. Two numbers or Boolean values are considered equal if they have the same value.

JScript copies, passes, and compares objects, arrays, and functions by reference. This process essentially creates a reference to the original item and uses the reference as if it were a copy. Changes to the original change both the original and the copy (and vice versa). There is really only one entity; the copy is just another reference to the data.

To successfully compare by reference, the two variables must refer to exactly the same entity. For example, two distinct Array objects will always compare as unequal, even if they contain the same elements. One of the variables must be a reference to the other one for the comparison to succeed. To check if two Arrays hold the same elements, compare the results of the toString() method.

Finally, JScript copies and passes strings by reference. Whether or not the strings are objects determines how the strings are compared. Two String objects (created with new String("something")) are compared by reference. If one (or both) of the strings is a literal or primitive string value, they are compared by value.

Data Comparison

JScript can compare data either by value or by reference. To perform a test by value, JScript compares two distinct items to determine whether they are equal to each other. Usually, this comparison is performed on a byte-by-byte basis. When it tests by reference, it checks to see whether two items refer to the same item. If they do, then they compare as equal; if not, although they may contain the exact same values, byte-for-byte, they compare as unequal.

Strings may be compared by value or by reference, depending on whether or not the strings are objects. If both strings are String objects, the strings are compared by reference; otherwise, they are compared by value. This allows two strings to compare as equal if each was created separately from the other but each has the same content. To compare the values of two String objects, first convert the objects to non-object strings with the toString or valueOf methods, and then compare the resulting strings. For more information, see JScript Assignments and Equality.

Function Parameters

When JScript passes a parameter to a function by value, it makes a separate copy of that parameter that exists only inside the function. Even though objects and arrays are passed by reference, if a new value in the function directly overwrites them, the new value is not reflected outside the function. Only changes to properties of objects, or elements of arrays, are visible outside the function.

For example, the following program has two functions. The first function overwrites the input parameter, which prevents further the changes to the parameter from affecting the original input argument. The second function changes the property of the object without overwriting the object.

JScript Conditional Structures

Statements in JScript are normally executed sequentially in the order in which they appear in the script. This is called sequential execution and is the default direction of program flow.

An alternative to sequential execution transfers the program flow to another part of a script as a result of conditions that a script encounters. That is, instead of executing the next statement in the sequence, a statement at another location is executed. Another alternative is called iteration, which involves repeating the same sequence of statements multiple times. Iteration is frequently achieved using loops.

JScript Reserved Words (Visual Studio - JScript)

JScript has a number of reserved words that have a specific meaning in the JScript language syntax. Consequently, these words are not good choices for names of functions, variables, or constants in scripts. There are three classes of reserved words.

break case catch class const continue debugger default delete do else export extends false finally for function if import in instanceof new null protected return super switch this throw true tryh typeof var while with abstract boolean byte char decimal double enum final float get implements int interface internal long package private protected public sbyte set short static uint ulong ushort void

Versions

JScript

The original JScript is an Active Scripting engine. Like other Active Scripting languages, it is built on the OLE Automation|COM/OLE Automation platform and provides scripting capabilities to host applications.

This is the version used when hosting JScript inside a Web page displayed by Internet Explorer, in an HTA (programming language)|HTML application, in Active Server Pages|classic ASP, in Windows Script Host scripts and several other OLE Automation|Automation environments.

JScript is sometimes referred to as "classic JScript" or "Active Scripting JScript" to differentiate it from newer .NET-based versions.

Some versions of JScript are available for multiple versions of Internet Explorer and Windows. For example, JScript 5.7 was introduced with Internet Explorer 7|Internet Explorer 7.0 and is also installed for Internet Explorer 6|Internet Explorer 6.0 with Windows XP XP service pack 3|Service Pack 3, while JScript 5.8 was introduced with Internet Explorer 8|Internet Explorer 8.0 and is also installed with Internet Explorer 6.0 on Windows Mobile#Windows Mobile 6.5|Windows Mobile 6.5.

Microsoft's implementation of ECMAScript 5th Edition in Windows 8#Consumer Preview|Windows 8 Consumer Preview is called JavaScript and the corresponding Visual Studio 11 Express Beta includes a “completely new”, full-featured JavaScript editor with IntelliSense enhancements for HTML5 and ECMAScript 5 syntax, “VSDOC” annotations for multiple overloads, simplified Document Object Model|DOM configuration, brace matching, collapsible outlining and “go to definition”.

Version Date Introduced with JScript supports various features not specified in the ECMA standard, Similar JavaScript]] version
1.0 Aug 1996 Internet Explorer 3.0]] Netscape JavaScript 1.0
2.0 Jan 1997 Windows IIS]] 3.0 Netscape JavaScript 1.1
3.0 Oct 1997 Internet Explorer 4.0]] Microsoft said JScript 3.0 was "the first scripting language to fully conform to the ECMA-262 standard". 1.3
4.0 Visual Studio]] 6.0 (as part of Visual InterDev]]) ECMA-262 1st edition 1.3
5.0 Mar 1999 Internet Explorer 5.0]] ECMA-262 2nd edition 1.4
5.1 Internet Explorer 5.01 ECMA-262 2nd edition 1.4
5.5 Jul 2000 Internet Explorer 5.5 & Windows CE]] 4.2 ECMA-262 3rd edition 1.5
5.6 Oct 2001 Internet Explorer 6.0]] & Windows CE]] 5.0 ECMA-262 3rd edition 1.5
5.7 Nov 2006 Internet Explorer 7.0]] JScript 5.7 includes an implementation of the ECMAScript Compact Profile (ECMA-327) which turns off features not required by the ES-CP when using the "JScript.Compact" ProgID. 1.5
5.8 Mar 2009 Internet Explorer 8.0]] & Internet Explorer Mobile|Internet Explorer Mobile 6.0]] 3]] 1.5
9.0 Mar 2011 Internet Explorer 9.0]] ECMA-262 5th edition 1.8.1

JScript is also available on Windows CE (included in Windows Mobile, optional in Windows Embedded CE). The Windows CE version lacks Active Debugging.

JScript .NET

Main article: JScript .NET

JScript .NET is a Microsoft .NET implementation of JScript, it is a Common Language Specification|CLS language and thus inherits very powerful features, but lacks many features of the original JScript language, making it inappropriate for many scripting scenarios. JScript .NET can be used for ASP.NET pages and for complete .NET applications, but the lack of support for this language in Microsoft Visual Studio place it more as an upgrade path for classic Active Server Pages|ASP using classic JScript than as a new first-class language.

Version Platform Date Introduced with Based on
7.0 CLR]] 1.0 2002-01-05 Microsoft .NET Framework]] 1.0 JScript .NET is "being developed in conjunction with ECMAScript Edition 4".
7.1 CLR]] 1.1 2003-04-01 Microsoft .NET Framework]] 1.1 ECMA-262 3rd edition
8.0 CLR]] 2.0 2005-11-07 Microsoft .NET Framework]] 2.0 ECMA-262 3rd edition
10.0 CLR]] 4.0 2010-08-03 Microsoft .NET Framework]] 4.0 ECMA-262 3rd edition

JScript .NET is not supported in the .NET Compact Framework.

Note: JScript .NET versions are not related to classic JScript versions. JScript .NET is a separate product. Even though JScript .NET is not supported within the Visual Studio IDE, its versions are in sync with other .NET languages versions (C Sharp (programming language)|C#, VB.NET, VC++) that follows their corresponding Visual Studio versions.

.NET Framework 3.0 and 3.5 are built on top of 2.0 and do not include the newer JScript.NET release (version 10.0 for .NET Framework 4.0).

References

  1. JScript
    1. https://msdn.microsoft.com/en-us/library/sz1tawa5%28v=vs.100%29.aspx