Language Features
Script combines JavaScript-like syntax with Rust-inspired memory safety and native code performance.
Variables & Types
let x = 42; // Number
let name = "Script"; // String
let active = true; // Boolean
let data = { key: 1 }; // Object
let items = [1, 2, 3]; // Array
Functions & Closures
// Function declaration
function greet(name) {
return "Hello, " + name + "!";
}
// Arrow functions
let double = (x) => x * 2;
let add = (a, b) => a + b;
// Closures
function counter() {
let count = 0;
return () => {
count = count + 1;
return count;
};
}
Control Flow
if (condition) {
// ...
} else {
// ...
}
for (let i = 0; i < 10; i++) {
// ...
if (done) break;
if (skip) continue;
}
while (condition) {
// ...
}
do {
// ...
} while (condition);
Objects & Arrays
let obj = { x: 10, y: 20 };
obj.z = 30;
console.log(obj["x"]);
let arr = [1, 2, 3];
arr.push(4);
let first = arr[0];
Classes & Inheritance
class Animal {
name: string;
constructor(name: string) {
this.name = name;
}
speak() {
console.log(this.name + " makes a sound");
}
}
class Dog extends Animal {
breed: string;
constructor(name: string, breed: string) {
super(name);
this.breed = breed;
}
speak() {
console.log(this.name + " barks!");
}
}
let dog = new Dog("Buddy", "Golden Retriever");
dog.speak(); // "Buddy barks!"
Private Fields
Script supports JavaScript-style private fields using the # prefix:
class Counter {
#count = 0; // Private field (only accessible within class)
increment() {
this.#count++;
}
getCount() {
return this.#count; // Can access private field from methods
}
}
let c = new Counter();
c.increment();
console.log(c.getCount()); // 1
// c.#count; // ERROR: Private field not accessible outside class
// c["#count"]; // Returns undefined (encapsulation works)
Error Handling
try {
riskyOperation();
} catch (e) {
console.log("Error: " + e);
} finally {
cleanup();
}
Template Literals
const name = "World";
const greeting = `Hello, ${name}!`; // "Hello, World!"
Decorators
Script supports TypeScript-style decorators:
function logged(target: any) {
console.log(`Decorating class: ${target.name}`);
return target;
}
@logged
class MyClass {
// ...
}
Type Annotations
Script supports TypeScript-style type annotations with Hindley-Milner inference:
let x: number = 42;
function add(a: number, b: number): number {
return a + b;
}
let arr: string[] = ["a", "b"];
// Generics
function identity<T>(x: T): T {
return x;
}
// Union types
let value: string | number = "hello";
// Type aliases
type Point = { x: number, y: number };
Async/Await
async function fetchData() {
let result = await someAsyncOperation();
return result;
}
// Promise handling
Promise.resolve(42).then((value) => {
console.log(value);
});
Modules
Script supports ES module syntax:
// Exporting
export function greet(name) {
return "Hello, " + name;
}
export const VERSION = "1.0.0";
// Importing
import { greet, VERSION } from "./greeting";
import * as utils from "./utils";
Ownership & Borrow Checking
Script includes Rust-inspired ownership semantics for memory safety:
// Ownership types
let owned: Ref<Object> = createObject();
let borrowed: MutRef<Object> = borrow(owned);
// Move semantics
let a = { value: 42 };
let b = a; // 'a' is moved to 'b'
// console.log(a); // Error: use after move
The borrow checker prevents data races and use-after-move errors at compile time.