Axional provides three script engines:

  • Nashorn
  • Graal
  • J2V8

You can select the script engine by using it's tag name in the type attribute of the script tag.

1 Nashorn

The Nashorn engine is an implementation of the ECMAScript Edition 5.1 Language Specification. It also implements many new features introduced in ECMAScript 6 including template strings; let, const, and block scope; iterators and for..of loops; Map, Set, WeakMap, and WeakSet data types; symbols; and binary and octal literals.

2 Graal

Graal high performance implementation of the JavaScript programming language. Built on the GraalVM by Oracle Labs.

The goals of GraalVM JavaScript are:

  • Execute JavaScript code with best possible performance
  • Full compatibility with the latest ECMAScript specification
  • Support Node.js applications, including native packages (check)
  • Allow simple upgrading from Nashorn or Rhino based applications
  • Fast interoperability with Java, Scala, or Kotlin, or with other GraalVM languages like Ruby, Python, or R

GraalVM JavaScript is compatible with the ECMAScript 2020 specification.

2.1 ES6 class

JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript's existing prototype-based inheritance.

2.1.1 Defining classes

Classes are in fact "special functions", and just as you can define function expressions and function declarations, the class syntax has two components: class expressions and class declarations.

Copy
<script type="graal">
    class Person {
    	constructor(name, age) {
    		this.name = name;
    		this.age = age;
    	}
    	introduction() {
    		return `My name is ${this.name} and I am ${this.age} years old!`;
    	}
    }
    
    let john = new Person("John Smith", 18);
    console.log(john.introduction());
</script>
My name is John Smith and I am 18 years old!
Copy
<script type="graal">
    class Student {
          constructor(rno,fname,lname){
             this.rno = rno
             this.fname = fname
             this.lname = lname
             console.log('inside constructor')
          }
          set rollno(newRollno){
             console.log("inside setter")
             this.rno = newRollno
          }
       }
       let s1 = new Student(101,'Sachin','Tendulkar')
       console.log(s1)
       //setter is called
       s1.rollno = 201
       console.log(s1)
</script>

2.1.2 Static methods

The static keyword defines a static method for a class. Static methods are called without instantiating their class and cannot be called through a class instance. Static methods are often used to create utility functions for an application.

Copy
<script type="graal">
    class Point {
      constructor(x, y) {
        this.x = x;
        this.y = y;
      }
    
      static distance(a, b) {
        const dx = a.x - b.x;
        const dy = a.y - b.y;
    
        return Math.hypot(dx, dy);
      }
    }
    
    const p1 = new Point(5, 5);
    const p2 = new Point(10, 10);
    
    console.log(Point.distance(p1, p2)); // 7.0710678118654755
</script>

2.1.3 Sub classing with extends

The extends keyword is used in class declarations or class expressions to create a class as a child of another class.

Copy
<script type="graal">
    class Animal { 
      constructor(name) {
        this.name = name;
      }
      
      speak() {
        console.log(this.name + ' makes a noise.');
      }
    }
    
    class Dog extends Animal {
      speak() {
        console.log(this.name + ' barks.');
      }
    }
    
    var d = new Dog('Mitzie');
    d.speak(); // Mitzie barks.
</script>

If there is a constructor present in subclass, it needs to first call super() before using "this".

2.1.4 Super class calls with super

The super keyword is used to call corresponding methods of super class.

Copy
<script type="graal">
    class Cat { 
      constructor(name) {
        this.name = name;
      }
      
      speak() {
        console.log(this.name + ' makes a noise.');
      }
    }
    
    class Lion extends Cat {
    
    
      speak() {
        super.speak();
        console.log(this.name + ' roars.');
      }
    }
    
    var l = new Lion('Fuzzy');
    l.speak(); 
</script>
Fuzzy makes a noise.
Fuzzy roars.