pxt-calliope/docs/js/lang.md
2016-07-02 10:18:45 -04:00

3.0 KiB

JavaScript and TypeScript

You can write micro:bit programs in a subset of TypeScript, a superset of JavaScript. Many micro:bit programs, especially at the beginner's level, are just plain JavaScript. TypeScript introduces class-based object-oriented programming, such as:

class Greeter {
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}

let greeter = new Greeter("world");
basic.showString(greeter.greet())

This site is meant for teaching programming first, and JavaScript second. For this reason, we have stayed away from concepts that are specific to JavaScript (for example, prototype inheritance), and instead focused on ones common to most modern programming languages (for example, loops, lexically scoped variables, functions, classes, lambdas).

We leverage TypeScript's type inference so that students need not specify types when clear from context.

Supported language features

User-defined types and modules

Control-flow constructs

  • if ... else if ... else statements
  • while and do ... while loops
  • for(;;) loops (see below about for ... in/of)
  • break/continue; also with labeled loops
  • switch statement (on numbers only)
  • debugger statement for breakpoints

Expressions

  • conditional operator ? :; lazy boolean operators
  • all arithmetic operators (including bitwise operators); note that in microcontroller targets all arithmetic is performed on integers, also when simulating in the browser
  • strings (with a few common methods)
  • string templates (`x is ${x}`)
  • arrow functions () => ...
  • array literals [1, 2, 3]

Unsupported language features

We generally stay away from the more dynamic parts of JavaScript. Things you may miss and we may implement:

  • exceptions (throw, try ... catch, try ... finally)
  • for ... of statements
  • object literals { foo: 1, bar: "two" }
  • method-like properties (get/set accessors)
  • class inheritance

If there is something you'd like to see, please file an issue at GitHub.