bhylu.com

Advertisements

JS Classes

A class groups a constructor and instance methods. New creates an instance and calls its constructor.

Where to run this

This example uses the JavaScript console playground. Select Run to execute it; the example does not run on this lesson page.

A practical example

JavaScript

class Member {
  constructor(name) { this.name = name; }
  greet() { return "Hello, " + this.name; }
}
console.log(new Member("Mira").greet());

What happens

The new member returns a greeting.