-
Notifications
You must be signed in to change notification settings - Fork 0
/
intermediateStart.js
129 lines (102 loc) · 3.66 KB
/
intermediateStart.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//introduction to classes
//classes allow us to make a template for objects
//and for each new object we can have different object properties
//they are a good way to reduce duplicate code and debugging time
//difference between making an object literal and a class object template
//classes contain a constructor
//both have getters and setters
//both can contain methods of behavior
//heres an example of a class that describes a Dog object
class Dog {
constructor(name) {
this._name = name;
this._behavior = 0;
}
get name() {
return this._name;
}
get behavior() {
return this._behavior;
}
incrementBehavior() {
this._behavior ++;
}
}
//here a new dog object is instantiated with the name 'Halley'
const halley = new Dog('Halley');
console.log(halley.name); // Print name value to console
console.log(halley.behavior); // Print behavior value to console
halley.incrementBehavior(); // Add one to behavior
console.log(halley.name); // Print name value to console
console.log(halley.behavior); // Print behavior value to console
//by convention, we capitalize class names
//now that we've made a class, we can make a class instance. an instance is an object that contains
//property names and methods of a class but with unique property values
const ruby = new Dog('Ruby') //this is a new instance of the Dog object where the name property is 'Ruby'
//classes can also contain methods (behaviors you can call off the objects)
//class method and getter syntax is same as it is for objects except you can not include commas between methods
//in javascript, we can also use inheritance
//heres a Cat object
class Cat {
constructor(name, usesLitter) {
this._name = name;
this._usesLitter = usesLitter;
this._behavior = 0;
}
get name() {
return this._name;
}
get usesLitter() {
return this._usesLitter;
}
get behavior() {
return this._behavior;
}
incrementBehavior() {
this._behavior++;
}
}
//notice how similar it is to our Dog object? we can consolidate coding space by using inheritance
//lets abstract their shared properties and methods into a parent class called Animal
class Animal {
constructor(name) {
this._name = name;
this._behavior = 0;
}
get name() {
return this._name;
}
get behavior() {
return this._behavior;
}
incrementBehavior() {
this._behavior++;
}
}
//now that we hae these shared properties and methods in a parent class,
//we can extend them to whatever subclass we desire
class Turtle extends Animal {
constructor(name,cleansShell){
super(name)
this._cleansShell=cleansShell
}
}//this create a new class named Turtle which shares methods and properties from the parent class Animal
//super is needed in the constructor to fulfill the parameters from the parent
//class' constructor
//then the new property is added underneath. this is unique for turtle
//'extends' is needed to relate a subclass to it's parent class
//we can also make static methods
//a static method you can call off the class name
class guy {
constructor(name) {
this._name = name;
this._behavior = 0;
}
static generateName() {
const names = ['John', 'Beaux', 'Logan', 'SnoopDogg', 'Chandler'];
const randomNumber = Math.floor(Math.random()*5);
return names[randomNumber];
}//we can call this method off of guy itself
}
console.log(guy.generateName()) //returns a name
//because this is a static method, we cannot access it from an instance of guy, only from the class itself