ECMAScript 4 - Java Script

The future of JavaScript is inescapably tied to the fourth edition of ECMAScript, which has met with substantial problems since first being proposed.

Technical Committee 39 (TC39), which you may remember as the group inside of ECMA that first standardized ECMAScript, is still in charge of developing future editions.Like the first edition of ECMAScript, the fourth edition was first proposed by Netscape Communications, and TC39’s schedule originally called for its release in 2002.However, issues arose surrounding the clout that Microsoft had gained on the Web since the first edition of ECMAScript had been standardized.Microsoft entered its own proposal to TC39 for the future direction of ECMAScript.

TC39 changed the planned release of ECMAScript Edition 4 to the first quarter of 2004, almost two years later than the originally scheduled release. However, March 2004 came and went without the release. As of the time of my writing, no indication has been given as to when the next edition of ECMAScript will be released. All schedules of record still indicate the Q1 2004 date.

Given this lack of direction from the ECMA, the only possible way to investigate the future of JavaScript is to take a look at the proposal that was sent to TC39 for consideration.

Netscape’s proposal

When Netscape submitted its proposal to TC39,the future of Netscape Communications looked bleak. Since that time, Netscape was purchased by Time Warner (then AOL Time Warner) and then summarily disbanded, leaving the future of its Web browser strictly to the open-source Mozilla Foundation. TheMozilla Foundation still supports the Netscape proposal and updates it periodically,although the last update noted was in June 2003.

Netscape’s proposal for ECMAScript would turn ECMAScript into a lightweight version of the Java programming language. The goals listed in the proposal are the following:

  • Making the language suitable for writing modular and object-oriented applications
  • Making it possible and easy to write robust and secure applications
  • Improving upon ECMAScript’s facilities for interfacing with a variety of other languages and environments
  • Improving ECMAScript’s suitability for writing applications for which performance matters
  • Simplifying the language where possible
  • Keeping the language implementation compact and flexible

Although these goals seem pretty ambitious,the proposal is quick to point out that its authors do not see ECMAScript as a replacement for C++ or Java, nor do they intend to push it in that direction.

In order to accomplish the goals listed previously, the Netscape proposal suggests a number of changes to the language from the third edition.These changes include:

  • Optional strict typing and type checking of values.As discussed, ECMAScript is currently a loosely typed language. The proposal asks to change this in order to cut down on errors and bring ECMAScript more in line with other object-oriented languages.
  • More logical syntax for classes. In the current edition of ECMAScript,a strict difference exists between defining a global function and defining a class. This proposal suggests the use of the reserved word class to make the syntax for defining classes more logical (and ultimately, more Java-like). This would include using the extends reserved word for more straightforward inheritance and the introduction of private and protected scopes.
  • Addition of more types. Because one of the main goals of the proposal is to allow for easier interaction with other languages,it is suggested that the fourth edition of ECMAScript include more types, such as integer and long, that are supported in other languages.

Keywords and reserved words

Netscape proposes the following keywords:

You will probably recognize some of these keywords as reserved words in ECMAScript Edition 3,which is why they are reserved.
Additionally,the proposal asks for the following reserved words:

abstract debugger enum goto implements interface native protected synchronized throws transient volatile

As you can see, this list of reserved words clearly shows the Netscape proposal moving ECMAScript more towards a Java-like syntax.

The following words were reserved in ECMAScript Edition 3, but are used as part of the language in Netscape’s proposal:

boolean byte char double final float int long short static

Finally,special meanings are attached to the words get and set, which cannot be used as variable names.

Variables

According to the Netscape proposal,variables can be defined with an explicit type by including a semicolon and the type name when defining the variable, like this:

var color : String = “red”;

In the previous code,a variable with name color is created as type String.Defining the type for a variable is optional under this proposal. This can also be used with classes you would define yourself:

var specialObject : MyClass = new MyClass();

A slight twist on variables is the capability to create true constant values that cannot be changed by using the const keyword:

const age = 32;

This code defines the variable age as a constant and assigns a value of 32.

Functions

Functions also take advantage of this new type declaration by providing types for its arguments and return value. Consider the following example:

Here, the function named sum() takes two parameters of type Integer and returns an Integer (as indicated by the semicolon after the closing parenthesis). If the function doesn’t return a value, it is defined as type Void:

Functions that have argument and return value types defined are type checked, although it is possible to leave off the types and have functions behave as they do in ECMAScript today.

Netscape’s proposal would make overloading of functions work in a manner similar to overloading in Java: Just define as many functions as necessary with different argument lists. For example, you could make two different versions of the sum() function defined earlier, one to take two arguments and one to take three:

Another change to functions is the capability to define named arguments,which are optional and can be assigned in any order so long as the name of the argument is used. For example, the following function contains two named arguments:

To call this function, you can use only two arguments, three arguments, or all four:

Note the last two lines where the named arguments are used; specifically notice the last line, where num4 is used before num3.

Numeric literals

In order to support more types of numbers, the Netscape proposal introduces three new types of numbers: long, unsigned long (ulong), and float. You can indicate the type of number by including an l (or L) for long values, ul (or uL, Ul, UL) for ulong values, and f (or F) for float values. For example, L25 is a long, UL25 is a ulong, and F25 is a float.

Types

Netscape’s proposal introduces several new types:

  • Never— Has no values
  • Void— Replaces the Undefined type from ECMAScript Edition 3
  • Integer— All integer values
  • char— All single 16-bit Unicode characters
  • Type— The supertype of all types

Unlike ECMAScript Edition 3, all types are considered objects and not primitive values.

New machine types represent primitive number values:

  • sbyte— Signed byte integer, values between –128 and 127
  • byte— Byte integer, values between 0 and 255
  • short— Short integer, values between –32768 and 32767
  • ushort— Unsigned short integer, values between 0 and 65535
  • int— Integer, values between –2147483648 and 2147483647
  • uint— Unsigned integer, values between 0 and 4294967295
  • long— Long integer, values between –9223372036854775808 and 9223372036854775807
  • ulong– Unsigned long integer, values between 0 and 18446744073709551615
  • float— Floating-point number, all single precision IEEE floating-point numbers

The machine types can be used in place of regular types whenever a number must be represented.For example:

Classes

A class in ECMAScript Edition 4 (per the Netscape proposal) takes a form like this:

As you can see, the class keyword is used to define MyClass. There is one private variable named color, one constructor, and one public method named sayColor().In ECMAScript Edition 4, both public and private are considered namespaces that define the context in which variables and methods can be accessed.Classes defined in this manner are instantiated in the same way as in ECMAScript Edition 3, by using the new keyword:

var myObject : MyClass = new MyClass();

Along with variables and methods, it is possible to define getters and setters for properties. The getter is called when the variable is used for the value it contains, whereas a setter assigns a new value to a given variable.

The previous code creates a property named myColor, which can be used just like a regular property (such as color),but whose behavior is defined by the getter and setter functions. For instance:

Typically, this would be done when you want to perform some other action after a property value changes (such as changing the color of a UI element when the myColor property changes). It is also possible to redefine the behavior of operators when they interact with classes.In ECMAScript Edition 3, all operators use an object’s valueOf() or toString() method, but here it is possible for you to define exactly how a plus or minus should be used with an instance of a particular class.

This class defines a private property named value, which can be initialized using the class constructor. The last function defines what happens when a plus is used with a MyClass object. This particular function defines the behavior when the second operand is also a MyClass object: The values of the two objects are added and a new MyClass object is returned. With this definition, it is possible to use the following line:

Finally, classes can have true static properties and methods by using the static keyword:

The static members of a class can then be accessed as you might expect:

var value : Integer = MyClass.value;

Inheritance

Inheritance in ECMAScript Edition 4 is done in the same way as in Java, by using the extends keyword. For example:

First,note that the extends keyword is used to inherit from ClassA to create ClassB. This is a much simpler and straightforward way to establish inheritance than the traditional ECMAScript method (although object masquerading and prototype chaining is still available for backwards compatibility). The second thing to note is the use of the super() method to use ClassA’s constructor in ClassB. Once again, this is identical to Java.

Implementations

Although ECMAScript Edition 4 isn’t an officially released standard,that hasn’t stopped both Microsoft and Mozilla from implementing some of it.

Mozilla

Mozilla chose to implement some small features in the Web browser. For instance,it is possible to define constant values in Mozilla using the const keyword:

const message = “Hello World!”;

This line causes an error in all other browsers because const is a reserved word in ECMAScript Edition 3. In Mozilla, however, this line defines a constant string named message that cannot have its value changed.

Mozilla also chose to implement getters and setters for object properties, albeit with a different syntax. You may remember getters and setters from earlier chapters of this book, but here is a brief example:

This is an alternate syntax (so as not to break syntax in non-Mozilla browsers):

Note that even though this example is syntactically correct, it causes an error in browsers other than Mozilla when it is executed because neither function (defineGetter() or defineSetter())is defined.

Microsoft

Microsoft hasn’t updated its browser-based implementation of JavaScript since Internet Explorer 5.5 was released. What it has done, however,is include a language called JScript.NET in the .NET Framework. JScript.NET is, for all intents and purposes, an implementation of ECMAScript Edition 4 with some Microsoft-specific additions added for good measure. However,it can’t be used as a client-side scripting language in Internet Explorer, only as a server-side language in ASP.NET or as a standalone application (yes, JScript.NET can be compiled).

Unlike other versions of JavaScript, JScript.NET is a compiled language capable of becoming a standalone executable file.It is compiled down to the same .NET machine code and executed using the same Common Language Runtime(CLR) as both Visual Basic.NET and C#. Although it is beyond the scope to discuss the full potential and scope of JScript.NET.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Script Topics