Introduction

AxScript is built on top of the JavaScript engine from Qt's JSEngine framework. AxScript shares the same syntax as JavaScript:

  • variable definition and basic operations

let a = 5 + 23;  // let - block environment
if (a > 12) {
    var b = 12;  // var - global (func) environment
}
ax.log(b)        // print: 12
  • Basic String operations

// Create
let str1 = "Hello";
let str2 = 'World';
let str3 = `Hello ${str2}`;     // Hello World

// Operations
"Hello" + " " + "World"	        // "Hello World"
"Hello".length	                // 5
"Hello"[1]	                // "e"
"Hello".includes("ell")	        // true
"Hello".replace("llo", "y")	// "Hey"
"a,b,c".split(",")	        // ["a", "b", "c"]
"Hello".slice(1, 3)	        // "el"
"Hello".toUpperCase()	        // "HELLO"

// ... And other string JS operations
  • Basic Array operations

// Create
let arr1 = [1, 2, 3];
let arr2 = new Array(1, 2, 3);
let arr3 = ["a", 42, true, { name: "John" }];

// Operations
arr1.length                 // 3
arr1[1]	                    // 20
arr1.push(10)	            // 4 (arr1: [1, 2, 3, 10])
arr1.pop()                  // 3 (arr1: [1, 2])
arr1.unshift(10)            // 4 [10, 1, 2, 3]
arr1.shift()                // 1 (arr1: [2, 3])
arr1.concat([4, 5])         // [1, 2, 3, 4, 5]
arr1.indexOf(2)             // 1
arr1.includes(2)	    // true
arr1.filter(x => x > 1)     // [2, 3]
arr1.map(x => x * 2)	    // [2, 4, 6]
[3, 1, 2].sort()	    // [1, 2, 3]
[1, 2, 3].reverse()	    // [3, 2, 1]
[1, 2, 3, 4].slice(1, 3)    // [2, 3]
arr1.splice(1, 1, 99)       // [2] (arr1: [1, 99, 3])

// ... And other array JS operations
  • Basic Map/Object operations

// Create
let obj1 = { name: "Alice", age: 25 };
let obj2 = new Object();
obj2.key = "value";

// Operations
obj1.name	                         // "Alice"
obj1["name"]	                         // "Alice"
obj1.city = "Tokyo"	                 // { name: "Alice", age: 25, city: "Tokyo" }
delete obj1.age	                         // { name: "Alice" }
"name" in obj1	                         // true
Object.keys(obj1)	                 // ["name", "age"]
Object.values(obj1)	                 // ["Alice", 25]
let newObj = { ...obj1 }	         // copy of
Object.assign({}, obj1, { city: "Tokyo" })  // {name: "Alice", age: 25, city: "Tokyo"}

// ... And other object JS operations
  • Function Declaration

// Classic
function greet(name) {
  return `Hello, ${name}!`;
}
ax.log(greet("Alice")); // "Hello, Alice!"


// Expression
const greet = function(name) {
  return `Hello, ${name}!`;
};
ax.log(greet("Bob")); // "Hello, Bob!"


// Arrow
const greet = (name) => `Hello, ${name}!`;
ax.log(greet("Charlie")); // "Hello, Charlie!"
  • Functions and parameters

// Default
function greet(name = "Guest") {
  return `Hello, ${name}!`;
}
ax.log(greet()); // "Hello, Guest!"


// Undefined number of arguments
function greet(greeting, ...names) {
  return `${greeting}, ${names.join(", ")}!`;
}
ax.log(greet("Hello", "Alice", "Bob", "Charlie"));  // "Hello, Alice, Bob, Charlie!"


// Arguments via object
function greet({ name, age }) {
  return `${name} is ${age} years old.`;
}
ax.log(greet({ name: "Alice", age: 25 })); // "Alice is 25 years old."

This is a small and incomplete example of what AxScript can do. For more information, see the Javascript reference.

Last updated