You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// The below is a constructor function. This allows us to set up unique objects and still let them be flexible
4
-
// You cannot use an arrow function for this as it does not bind this.
5
-
constPerson=function(firstName,lastName,age,likes=[]){// If likes isn't provided then create a default value
6
-
this.firstName=firstName
7
-
this.lastName=lastName
8
-
this.age=age
9
-
this.likes=likes
10
-
}
11
-
12
-
//------ Prototypal Inheritance ------
13
-
// Prototype adds this to all the instances
14
-
Person.prototype.getBio=function(){
15
-
letbio=`${this.firstName} is ${this.age}.`
16
-
17
-
// We are able to use an arrow function because the parent is able to access the this statement.
18
-
// Arrow functions don't bind this.
19
-
this.likes.forEach((like)=>{
20
-
bio+=` ${this.firstName} likes ${like}`
21
-
})
22
-
23
-
returnbio;
24
-
}
25
-
26
-
Person.prototype.setName=function(fullName){
27
-
constnames=fullName.split(' ')// This .split allows us to split a string and make an array. We're currently splitting on a space. This would create just Andrew
28
-
this.firstName=names[0]
29
-
this.lastName=[1]
1
+
// Prototypal Inheritance
2
+
3
+
//---- Class Syntax -----
4
+
// What makes the person class is the new way to create a constructor function. You define the constructor inside the class instead of inside the function.
5
+
// We never need to manually call the constructor - it gets called automatically when we use the new operator with the class name picked out
6
+
classPerson{
7
+
8
+
constructor(firstName,lastName,age,likes=[]){
9
+
this.firstName=firstName;
10
+
this.lastName=lastName;
11
+
this.age=age;
12
+
this.likes=likes;
13
+
}
14
+
15
+
getBio(){
16
+
letbio=`${this.firstName} is ${this.age}.`;
17
+
18
+
this.likes.forEach(like=>{
19
+
bio+=` ${this.firstName}likes${like}`;
20
+
});
21
+
22
+
returnbio;
23
+
}
24
+
25
+
setName(fullName){
26
+
constnames=fullName.split(" ");// This .split allows us to split a string and make an array. We're currently splitting on a space. This would create just Andrew
27
+
this.firstName=names[0];
28
+
this.lastName=[1];
29
+
}
30
30
}
31
31
// ---------------------
32
32
// The below creates a new person and logs it to the console.
0 commit comments