-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path032-Class.js
61 lines (52 loc) · 1.3 KB
/
032-Class.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
/*
* Author : Jaydatt Patel
class syntax:
1) class class_name{
variable declaration...;
constructor(){.........}
methods(){........}
}
2) const class_name = class {
variable declaration...;
constructor(){.........}
methods(){........}
}
You can also add prototype for class same as constructor function.
*/
class Vehicle {
// variable declaration
name;
//constructor
constructor(name = "Global", col = "white", spd = 200) {
this.name = name;
this.color = col; //color var automatically create using constructor
this.speed = spd; //speed var automatically create using constructor
}
//method-1
show() {
console.log(
`name: ${this.name}, color: ${this.color}, speed: ${this.speed}`
);
}
//method-2
turbo() {
console.log("Turbo On");
}
getSelf() {
console.log(this);
}
getPrototype() {
var proto = Object.getPrototypeOf(this);
console.log(proto);
}
}
console.log("Type :", Vehicle);
var car1 = new Vehicle();
car1.show();
car1.turbo();
var car2 = new Vehicle("Car", "Red", 150);
car2.show();
console.log("car2.getSelf() : ");
car2.getSelf();
console.log("car2.getPrototype() : ");
car2.getPrototype();