JavaScript

Language Extensions
Array.from( arrayLikeObject ) static
Function.prototype.subclass( superConstructor )
Function.abstractMethod() static
Number.prototype.toInt()
Number.prototype.toLong()
Number.prototype.toFloat()
String.prototype.trim()
String.prototype.trimLeft()
String.prototype.trimRight()
String.prototype.replaceAll( pattern, replacement )
String.prototype.startsWith( pattern )
String.prototype.endsWith( pattern )
Object.prototype.dontEnum( methodNames... )
RegExp.quote( string ) static
RegExp.quoteReplacement( string ) static

Language Extensions

This document describes extensions to the standard JavaScript language. These are defined in the common library, but grouped together here for convenience. There is no need to import this file.

Array.from( arrayLikeObject ) static

Create a JavaScript Array object from any "array-like" object, including a Java array. An array-like object a includes a length property and a sequence of zero or more sequential properties a[0], a[1], a[2], ... a[ a.length-1 ].

returns an Array containing a copy of the elements of arrayLikeObject

Function.prototype.subclass( superConstructor )

Makes objects created with the calling constructor a "subclass" of the superConstructor by chaining constructor.prototype to superConstructor.prototype.

Example:

 function Super() {
     this.x = "super";
 }

 Super.prototype.getX = function getX() {
     return this.x;
 }

 function Sub() {
     Super.call( this );
     this.x = "sub";
 }

 Sub.subclass( Super );

 var subobj = new Sub();
 // this will use the getX function defined for Super
 // (i.e. Super.prototype.getX)
 println( subobj.getX() );

constructor the name of the subclass constructor function
superConstructor the name of the superclass constructor function

Function.abstractMethod() static

When called, prints a standard warning message if script warnings are enabled. This function is not meant to be called directly. Rather, this function is assigned to a property in an object's prototype to make that function abstract. When a subclassing object fails to override the abstract method, calls to the abstract method will thus display a warning message for the developer.

Number.prototype.toInt()

Converts this number to a Java Integer object. Any fractional part is discarded. If the number lies outside the range of possible Integer values, an exception is thrown.

returns this number as a java.lang.Integer

Number.prototype.toLong()

Converts this number to a Java Long object. Any fractional part is discarded. If the number lies outside the range of possible Long values, an exception is thrown.

returns this number as a java.lang.Long

Number.prototype.toFloat()

Converts this number to a Java Float object. If the number lies outside the range of possible Float values, an exception is thrown.

returns this number as a java.lang.Float

String.prototype.trim()

Removes leading and trailing whitespace from this string.

returns this string with whitespace removed from both ends

String.prototype.trimLeft()

Removes leading whitespace from this string.

returns this string with whitespace removed from the start

String.prototype.trimRight()

Removes trailing whitespace from this string.

returns this string with whitespace removed from the end

String.prototype.replaceAll( pattern, replacement )

Replaces every occurrence of pattern with replacement, where both values are plain strings rather than regular expressions. The replacements are made as if by processing the string from left to right and replacing each time a match is possible. For example, the following code results in bb rather than bbb:
 println( "aaaa".replaceAll( "aa", "b" ) );

pattern the substring to replace all occurrences of
replacement the substring to replace each occurrence with

returns the replaced string

String.prototype.startsWith( pattern )

Returns true if pattern is a prefix of this string.
 "meta".startsWith( "me" ); // true
 "Random".startsWith( "and" ); // false

pattern the string to match against the start of this string

returns true if and only if this string starts with pattern

String.prototype.endsWith( pattern )

Returns true if pattern is a suffix of this string.
 "bartend".endsWith( "tend" ); // true
 "moose".endsWith( "oos" ); // false

pattern the string to match against the end of this string

returns true if and only if this string ends with pattern

Object.prototype.dontEnum( methodNames... )

Prevents the named methods from being enumerated. This is most useful when extending Object's prototype with new functions, as it maintains the ability to use plain objects as a map.
 var obj = {};
 Object.prototype.blight = function blight() {};
 println( "Before:" );
 for( let i in obj ) println(i);
 Object.prototype.dontEnum( "blight" );
 println( "After:" );
 for( let i in obj ) println(i);

RegExp.quote( string ) static

Escapes the characters in a string that have special meaning in a regular expression. If a regular expression is created from the string, it will match string literally. For example, * would be replaced with \\* because * has special meaning in a regular expression.

string the string to escape

returns a string that encodes string as regular expression

RegExp.quoteReplacement( string ) static

Escapes the characters in a string that have special meaning when used as a replacement for a regular expression.

string the string to escape
returns a string that encodes string as regular expression replacement

Contents