src/nodejs/jsclasses

    Dark Mode
Search:
Group by:

Example: cmd: -b:js -d:nodejs --experimental:strictFuncs

import src/nodejs/jsclasses


class Kitten* of JsRoot:

  proc constructor*(name: cstring; age: cint; food: float; sleeping: bool) =
    this.name = name
    this.age = age
    this.food = food
    this.sleeping = sleeping

  proc get_name*(): string =  # Or getArea()
    return this.name

  proc static_age*(): cint =  # Static method.
    return this.age

  proc calcAge*(): cint =
    return this.age + 1

  func goToSleep*() =
    this.sleeping = this.sleeping == true

  proc feed() =
    this.food = this.food + 1.0


let cat = newKitten(name="Zoe", age=2, food=1.0, sleeping=false)
discard cat.get_name()
discard cat.static_age()
cat.goToSleep()
cat.feed()
Generated JavaScript Example
/** Kitten Class (line 268, column 14). */
class Kitten {
  
  /** Kitten Constructor (line 270, column 4). */
  constructor(name, age, food, sleeping) {
    this.name = name;
    this.age = age;
    this.food = food;
    this.sleeping = sleeping;
  };
  
  /** Kitten.name Getter (line 276, column 4). */
  get name() {
    return this.name;
  };
  
  /** Kitten.static age Method (line 279, column 4). */
  static age() {
    return this.age;
  };
  
  /** Kitten.calcAge Method (line 282, column 4). */
  calcAge() {
    return this.age + 1;
  };
  
  /** Kitten.goToSleep Method (line 285, column 4). */
  goToSleep() {
    this.sleeping = this.sleeping == true;
  };
  
  /** Kitten.feed Method (line 288, column 4). */
  #feed() {
    this.food = this.food + 1.0;
  };

};
export { Kitten };

Macros

macro class(head, body: untyped)
Warning: Inheritance and Generators not supported.