Creating a new class
The most fundamental task class can be used for is, of course, creating a new class. This recipe shows the simple syntax for defining and instantiating a new class.
Getting ready
This recipe assumes you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named
07-01-create-a-new-class. - Copy or create an
index.htmlthat loads and runs amainfunction frommain.js. - Create a
main.jsfile that defines a new class namedRocketand amainfunction that creations two instances and logs them out:
// main.js
class Rocket {}
export function main() {
const saturnV = new Rocket();
const falconHeavy = new Rocket();
console.log(saturnV);
console.log(falconHeavy);
} - Start your Python web server and open the following link in your browser:
http://localhost:8000/. - You will see the following...