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:
Block scoping.
varwill 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.void. The
voidkeyword has been changed to be used as a type annotation. In JavaScript, thevoidkeyword was used for evaluating an expression and returningundefined. This can be accomplished by creating a function that returnsvoidin JS++.undefined.
undefinedis a keyword in JS++. JavaScript, in ECMAScript 3, allowsundefinedto be defined and set to any value; this is becauseundefinedis not a keyword in JavaScript but a property of the global object. By makingundefineda keyword, this is not an issue with JS++.Semicolons. JavaScript has a feature known as Automatic Semicolon Insertion (ASI). In some cases, it's possible to produce unforeseen bugs, such as:
123456functionfoo() {return// semicolon automatically inserted here1;// 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 returnsundefinedbecause a semicolon was automatically inserted after thereturnkeyword but before the1value. Therefore, the1never gets evaluated, and the function returnsundefined.JS++ always requires statements to be terminated by a semicolon.
Implicit Globals. JavaScript has "implicit" globals whereby if a variable is not declared with the
varkeyword, it becomes a global variable. For example:12varx = 1;// not globaly = 1;// globalAll 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.
ReferenceError. Besides implicit globals, JavaScript also generates a runtime
ReferenceErrorwhen a variable is accessed but not declared. Implicit globals happen on a write operation (assignment), whileReferenceErrors occur on read operations (access). Among other issues, this can cause an application to throw an exception on mistakes such as typos; for example:123varfree =true;varcopy = freee;// Typo. ReferenceError ends the program.foobar();// This never runs because the program has terminated with an uncaught exception.In JS++, a
ReferenceErrorcannot occur because all read/write operations on undeclared variables are compile-time errors.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
Stringobject has both asubstringandsubstrmethod, 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#sortmethod 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
