-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.js
More file actions
38 lines (30 loc) · 807 Bytes
/
classes.js
File metadata and controls
38 lines (30 loc) · 807 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Javascript classes ES6
// first way is a class declaration
// Use this one more commonly
// Every Class must have a constructor
// class Person {
// }
class Person {
constructor(name, hobby) {
this.name = name;
this.hobby = hobby;
}
// this is a prototype (method)
greeting() {
console.log(`Hello my name is ${this.name}`);
}
// this is a prototype (method)
myHobby() {
console.log(`My fav hobby is ${this.hobby}`);
}
// static method and this live on the class Person so you have to use Person.saySomething()
// instead of using the prototypes
static saySomething() {
console.log('Coding is so cool!');
}
};
const person1 = new Person('Jack', 'Coding');
const person2 = new Person('Jill', 'Skydiiving');
// second way is a class expression
// const Person = class {
// }