src/happyx/sugar/js

Search:
Group by:

JS ✨

Provides JS2Nim bridge

You can write PURE JS in PURE Nim with it ✌

Example

var myNimVariable = 10

buildJs:
  # JavaScript starts here
  function myJsFunction(a, b, c):
    # Embedded Nim code
    nim:
      echo "Hello, world!"
    # console.log(myNimVariable)
    echo ~myNimVariable
  
  class Animal:
    x: int
    constructor(x):
      self.x = x

Macros

macro buildJs(body: untyped): untyped

With this macro you can use PURE JavaScript in PURE Nim 👑

Available only on JS backend ✌

Supported syntax statements 👀

  • IF-ELIF-ELSE
  • CASE-OF
  • FOR/WHILE
  • variable/constant declaration

Also you can use ES6 classes 🥳

class Animal:
  say():
    discard

class Cat extends Animal:
  say():
    echo "Meow"

class Dog extends Animal:
  say():
    echo "Woof!"

var dog = new Dog();
var cat = new Cat();
dog.say();
cat.say();

This translates into 💻

class Animal {
  say() {
  }
}
class Cat extends Animal {
  say() {
    console.log("Meow");
  }
}
class Dog extends Animal {
  say() {
    console.log("Woof!");
  }
}
let dog = new Dog();
let cat = new Cat();
dog.say();
cat.say();