Classes Flashcards
1
Q
Create a class object
A
class MyClass { // class methods constructor() { ... } method1() { ... } method2() { ... } method3() { ... } ... }
EXAMPLE: class User { constructor(name) { this.name = name; } sayHi() { alert(this.name); } }
let user = new User("John"); user.sayHi();
When new User(“John”) is called:
A new object is created.
The constructor runs with the given argument and assigns it to this.name.
…Then we can call object methods, such as user.sayHi().
//constructor is run automatically. So you can do things like this.user = user