Getting Started

Private class

The javascript default field is public (public), if you need to indicate private, you can use (#)

class Dog {
  #name;
  constructor(name) {
    this.#name = name;
  }
  printName() {
    // Only private fields can be called inside the class
    console.log(`Your name is ${this.#name}`)
  }
}
const dog = new Dog("putty")
//console.log(this.#name)
//Private identifiers are not allowed outside class bodies.
dog.printName()

#Static private class

class ClassWithPrivate {
  static #privateStaticField;
static #privateStaticFieldWithInitializer = 42;
  static #privateStaticMethod() {
    // …
  }
}

kind

class Circle extends Shape {

#Constructor

constructor (radius) {
  this.radius = radius
}

#method

getArea () {
  return Math.PI *2 *this.radius
}

#Call the superclass method

expand(n) {
  return super.expand(n) *Math.PI
}

#Static methods

static createFromDiameter(diameter) {
  return new Circle(diameter /2)
}

Syntactic sugar for prototypes. See: classes

New library additions

#New string methods

"hello".repeat(3)
"hello". includes("ll")
"hello". startsWith("he")
"hello".padStart(8)     // "hello"
"hello".padEnd(8)       // "hello"
"hello".padEnd(8, '!')  // hello!!!
"\u1E9B\u0323".normalize("NFC")

#New Number Methods

Number.EPSILON
Number.isInteger(Infinity) // false
Number.isNaN("NaN")         // false

#New Math methods

Math.acosh(3)    // 1.762747174039086
Math.hypot(3, 4)  // 5
Math.imul(Math.pow(2, 32) -1, Math.pow(2, 32) -2) // 2

#New Array methods

//return a real array
Array.from(document.querySelectorAll("\*"))
//similar to new Array(...), but without the special single-argument behavior
Array.of(1, 2, 3)

See: New library additions

Exponential Operator

const byte = 2 **8

Same as: Math.pow(2, 8)

Binary and octal literals

let bin = 0b1010010
let oct = 0o755

See: Binary and Octal Literals

Template Strings

#Interpolation

const message = `Hello ${name}`

#Multi-line string

const str = `
hello
the world
`

Templates and multiline strings. See: template strings

Block-scoped

#Let

function fn () {
  let x = 0
  if (true) {
    let x = 1 // only inside this `if`
  }
}

#Const

const a = 1

let is the new var. Constants (const) work just like let, but cannot be reassigned. See: Let and const

Comments