Skip to content

Commit 8feb3fd

Browse files
authored
Merge pull request #283 from jcespinosa/classes-issue-186
Add information about Classes
2 parents cfab391 + 0735fc8 commit 8feb3fd

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

docs/JavaScript_Basics/classes.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,166 @@
11
# Classes
2+
3+
JavaScript is a programming language based on **prototypes**.
4+
Classes represent an improvement to the prototype-based inheritance, and provides a clearer and simpler syntax for creating objects and a new way to deal with inheritance.
5+
6+
> By definition, a class represents a template for the creation of objects, they provides the values ​​for the initial state (variables) and the implementation of the behavior (functions or methods).
7+
8+
## How to define a class
9+
10+
One way to define a class is through a **[class declaration](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class)**, the syntax is as follows:
11+
12+
```javascript
13+
class Student {
14+
  builder() {...}
15+
16+
  methodA() {...}
17+
18+
  methodB() {...}
19+
}
20+
```
21+
22+
The reserved word `class` is used, followed by the class name (`Student`)
23+
24+
It is also possible to define a class through a **[class expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/class)**, which can be **anonymous** or **named**, the syntax is as follows:
25+
26+
```javascript
27+
// Anonymous
28+
const Student = class {
29+
  builder() {...}
30+
31+
  methodA() {...}
32+
33+
  methodB() {...}
34+
}
35+
36+
// Named
37+
const Student = class TheStudent {
38+
  builder() { ... }
39+
40+
  methodA() {...}
41+
42+
  methodB() {...}
43+
}
44+
```
45+
46+
> The maiin difference between a **declaration** and an **expression** is that an **expression** creates a local scope; this means that the class name `TheStudent` will only be visible and usable within the class body.
47+
48+
## How to instantiate a class
49+
50+
To instantiate a class, use the reserved keyword [`new`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new), in this way a new object of the class type will be created.
51+
52+
```javascript
53+
let Bill = new Student();
54+
```
55+
56+
## Class body
57+
58+
Within the body of the class, there is a special method called [`constructor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor), this special method is called every time the class is instantiated to create a new object, and serves to define the initial state of the created object, for instance:
59+
60+
```javascript
61+
class Student {
62+
  // onlyname; // This should not have let or const only in classes
63+
  constructor(name, age) { // name and age are arguments given to object at the time of creation of object
64+
    this.name = name; // This initializes the local variable as name passed in argument
65+
    this.age = age; // This initializes the local variable as age passed in argument
66+
  }
67+
}
68+
69+
const Swapnil = new Student("Swapnil", 19); // This way we can create new objects with arguments
70+
```
71+
72+
The previous example defines a class of type `Student`, which receives two arguments in the constructor: `name` and `age`, both arguments will define the initial state of the object in two internal variables of the class (called in the same way) `this.name = name` and `this.age = age`.
73+
74+
> The body of the class is all the code that is between the curly braces `{}`.
75+
76+
77+
## Class methods
78+
79+
Class methods are defined as functions within the body of the class:
80+
81+
```javascript
82+
class StudentInfo {
83+
  // college = "SIES"; // This is allowed above ES7, ES8
84+
  constructor(name) {// name and age are arguments given to object at the time of creation of object
85+
    this.name = name; // This initializes the local variable as name passed in argument
86+
    this.college = "SIES"; // We want the College to be same for all students that's why it is declared outside of constructor
87+
  }
88+
89+
  getNameAndCollege() {// This is a method in Student
90+
    console.log(`${this.name} ${this.college}`);
91+
  }
92+
}
93+
```
94+
95+
In the example, a method called `getNameAndCollege` has been defined, to invoke the method it's necessary to instantiate the class to create an object, and then perform the method call, as follows:
96+
97+
```javascript
98+
const SwapnilInfo = new StudentInfo("Swapnil Bio");
99+
SwapnilInfo.getNameAndCollege();
100+
```
101+
102+
### Static methids
103+
104+
It's also possible to define define **[static methods](https://developer.mozilla.org/en-US/docs/Glossary/Static_method)**, [static methods](https://developer.mozilla.org/en-US/docs/Glossary/Static_method) can be called without needing to instantiate the class, for instance:
105+
106+
```javascript
107+
class StudentInfo {
108+
  constructor(name) {
109+
    this.name = name;
110+
    this.college = "SIES";
111+
  }
112+
113+
  getNameAndCollege() {
114+
    console.log(`${this.name} ${this.college}`);
115+
  }
116+
117+
  static getGreeting() {
118+
    console.log('Hello world'!);
119+
  }
120+
}
121+
122+
StudentInfo.getGreeting();
123+
// logs on console "Hello world!"
124+
```
125+
126+
The `getGreeting` method will log `Hello world!` in the console without creating an object with the keyword [`new`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new).
127+
128+
### Getters and Setters
129+
130+
It's also possible to define two types of special methods: **[getters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get)** and **[setters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set)**.
131+
**[Setters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set)** help us to assign a value to a class variable, and since it is a function, it is possible to add extra logic in the method as necessary.
132+
**[Getters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get)** help us to retrieve the value of a class variable, and same as **[setters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set)**, they allow us to define some extra logic inside the method as necessary.
133+
134+
```javascript
135+
class StudentInfo {
136+
constructor() { }
137+
138+
  set name(name) {
139+
    this.name = name.charAt(0).toUpperCase() + name.slice(1);
140+
  }
141+
142+
  get name() {
143+
    console.log (`${this.name}`);
144+
  }
145+
146+
  set college(college) {
147+
    this.college = college.toUpperCase();
148+
  }
149+
150+
  get college() {
151+
    console.log (`${this.name}`);
152+
  }
153+
154+
  static getGreeting() {
155+
    console.log ('Hello world');
156+
  }
157+
}
158+
159+
let BillInfo = new StudentInfo();
160+
161+
BillInfo.name = "bill";
162+
console.log (BillInfo.name); // Logs "Bill" because the extra logic capitalize the first letter
163+
164+
BillInfo.college = "sies";
165+
console.log (BillInfo.college); // Logs "SIES" because the extra logic capitalizes the whole string
166+
```

0 commit comments

Comments
 (0)