-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClasses.js
107 lines (78 loc) · 2.94 KB
/
Classes.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
//* Classes - JavaScript
/* In JavaScript, classes provide a clearer syntax and a more object-oriented way of
work with functions and prototypes. The classes were introduced in EcmaScript 6 (also known
such as Es6 or Es2015) and provide a more structured way to implement object-oriented programming
(Poo). Here's a basic introduction to classes in JavaScript: */
//? Declaration of a Class:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hello, I'm ${this.name}`);
}
}
// Create an instance of the class
let john = new Person("John", 30);
// Call a method of the class
john.greet(); // Display "Hello, I'm Juan" in the console
//? Builder:
/* The `constructor` method is called automatically when a new instance of the class is created. Here,
It is used to initialize the properties of the class. */
//? Methods:
/* Methods are functions that are associated with the class. They can be called in instances of the
class. */
//? Properties:
/* Properties are variables associated with the class and are defined within the constructor. */
//? Inheritance:
// You can extend an existing class to create a new class that inherits its properties and methods.
class Student extends Person {
constructor(name, age, course) {
super(name, age); // Call the base class constructor
this.course = course;
}
study() {
console.log(`${this.name} is studying ${this.course}`);
}
}
// Create an instance of the derived class
let studentJohn = new Student("John", 20, "Mathematics");
// Call methods of the base class and the derived class
studentJohn.greet(); // Displays "Hello, I'm Juan"
studentJohn.study(); // Sample "Juan is studying Mathematics"
//? Static Methods:
/* Static methods belong to the class itself and not to instances of the class. They are called in
class, not in an instance. */
class Utilities {
static add(a, b) {
return a + b;
}
}
// Call a static method
console.log(Utilities.sum(3, 4)); // Display 7 in the console
//? Getter and Setter:
// You can define `get` and `set` methods to access and modify properties in a controlled way.
class Rectangle {
constructor(base, height) {
this._base = base;
this._height = height;
}
getarea() {
return this._base * this._height;
}
set base(newBase) {
if (newBase > 0) {
this._base = newBase;
} else {
console.error("Base must be greater than zero");
}
}
}
let myRectangle = new Rectangle(5, 10);
console.log(myRectangle.area); // Display 50 in the console
myRectangle.base = 8; // Set a new base
console.log(myRectangle.area); // Display 80 in the console
/* These are some of the basic characteristics of classes in JavaScript. The classes provide
a more structured and readable way of working with object-oriented programming compared
with the old model based on prototypes. */