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
* Constructor pattern is used to create specific types of objects.
5
+
*/
6
+
7
+
// Basic Constructors
8
+
functionCar(model,year,miles){
9
+
this.model=model;
10
+
this.year=year;
11
+
this.miles=miles;
12
+
13
+
this.toString=function(){
14
+
returnthis.model+" has done "+this.miles+" miles ";
15
+
};
16
+
}
17
+
18
+
varcivic=newCar("Honda Civic",2020,20000);
19
+
console.log(civic);
20
+
console.log(civic.toString());
21
+
22
+
// Problems: The simple version of constructor is difficult to make inheritance, other is that functions such as toString() are redefined for each of the new object created using the Car constructor.
23
+
24
+
// Constructors with prototypes
25
+
functionCar2(model,year,miles){
26
+
this.model=model;
27
+
this.year=year;
28
+
this.miles=miles;
29
+
}
30
+
31
+
Car2.prototype.toString=function(){
32
+
returnthis.model+" has done "+this.miles+" miles ";
0 commit comments