Compatibility with JavaScript

JS++ is both backward- and forward-compatible with JavaScript (specifically, ECMAScript 3).

Backward compatibility is achieved with the external keyword. Forward compatibility can be achieved with user-defined types.

JS++ diverges from JavaScript beginning with the ECMAScript 3 standard in order to achieve maximum compatibility with legacy code bases.

JS++ also changes some of the syntax and semantics of JavaScript, including:

  1. Block scoping. var will create a block-scoped variable. JS++ does not distinguish between a variable that is block-scoped and a variable that is function-scoped; all variables are block-scoped.

  2. void. The void keyword has been changed to be used as a type annotation. In JavaScript, the void keyword was used for evaluating an expression and returning undefined. This can be accomplished by creating a function that returns void in JS++.

  3. undefined. undefined is a keyword in JS++. JavaScript, in ECMAScript 3, allows undefined to be defined and set to any value; this is because undefined is not a keyword in JavaScript but a property of the global object. By making undefined a keyword, this is not an issue with JS++.

  4. Semicolons. JavaScript has a feature known as Automatic Semicolon Insertion (ASI). In some cases, it's possible to produce unforeseen bugs, such as:

    1
    2
    3
    4
    5
    6
    function foo() {
        return       // semicolon automatically inserted here
            1;       // this expression is never reached
    }
     
    foo(); // returns 'undefined'

    The above code intends to return the numeric value 1, but, due to Automatic Semicolon Insertion, it actually returns undefined because a semicolon was automatically inserted after the return keyword but before the 1 value. Therefore, the 1 never gets evaluated, and the function returns undefined.

    JS++ always requires statements to be terminated by a semicolon.

  5. Implicit Globals. JavaScript has "implicit" globals whereby if a variable is not declared with the var keyword, it becomes a global variable. For example:

    1
    2
    var x = 1; // not global
    y = 1; // global

    All assignments to a variable identifier that have not been declared will declare the variable as a global in JavaScript. In the example above, "y" was not declared as a variable and will therefore become a global variable.

    JS++ requires variables to be declared before they can be assigned.

  6. ReferenceError. Besides implicit globals, JavaScript also generates a runtime ReferenceError when a variable is accessed but not declared. Implicit globals happen on a write operation (assignment), while ReferenceErrors occur on read operations (access). Among other issues, this can cause an application to throw an exception on mistakes such as typos; for example:

    1
    2
    3
    var free = true;
    var copy = freee; // Typo. ReferenceError ends the program.
    foobar(); // This never runs because the program has terminated with an uncaught exception.

    In JS++, a ReferenceError cannot occur because all read/write operations on undeclared variables are compile-time errors.

  7. Standard Library. While JavaScript does not have a "standard library" as such, the JS++ Standard Library implements all of the standard objects and functions defined for the ECMAScript Built-in Global Objects. However, JS++ made some minor tweaks. For instance, the JavaScript String object has both a substring and substr method, and these methods perform different actions which are not readily apparent based on the names. Instead, JS++ provides:

    string substring(int indexA, int indexB)
    string substringFor(int start, int length)

    In another example, JavaScript's Array#sort method will sort by strings by default, and custom logic needs to be written to sort numerically. In JS++, System.Array.sort() will sort numerically for numeric arrays.

See Also

Share

HTML | BBCode | Direct Link