-
Notifications
You must be signed in to change notification settings - Fork 0
/
class28-object-oriented-in-javascript.js
94 lines (70 loc) · 2.46 KB
/
class28-object-oriented-in-javascript.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
// We can declare a class in javascript like above
function Book(title, pages, isbn) { // {1}
this.title = title;
this.pages = pages;
this.isbn = isbn;
}
var book = new Book("My Title", "200", "100");
function Car(model, age, colour) { // {2}
this.model = model;
this.age = age;
this.colour = colour;
this.turnOn = function() {
console.log("The " + this.model + " car in turned on");
}
}
var car = new Car("Ferrari", "2019", "Black")
car.turnOn() // The Ferrari car in turned on
Book.prototype.printTitle = function() {
console.log(this.title);
};
// We can simplify using this code above:
class Book { // class name
constructor(title, pages, isbn) { // constructor
this.title = title;
this.pages = pages;
this.isbn = isbn;
}
printIsbn() { // method
console.log(this.isbn);
}
}
// To create a object
let book = new Book('title', 'pag', 'isbn');
console.log(book.title); // outputs the book title
book.title = 'new title'; // update the value of the book title
console.log(book.title); // outputs the book title
console.log(typeof(book)); // Object
// Inheritance
class ITBook extends Book { // {1}
constructor(title, pages, isbn, technology) {
super(title, pages, isbn); // use of the super constructor
this.technology = technology;
}
printTechnology() {
console.log(this.technology);
}
}
let jsBook = new ITBook('Learning JS Algorithms', '200', '1234567890', 'JavaScript');
console.log(jsBook.title); // output Learning JS Algorithms
console.log(jsBook.printTechnology()); // output JavaScript
console.log(jsBook.printIsbn()); // print the Isbn inherit from class Book method
// Getters and Setters
class Person {
constructor(name) {
this._name = name; //use of _atributte -> private attribute
}
get name() { // // use of get
return this._name;
}
set name(value) { // use of set
this._name = value;
}
}
let lotrChar = new Person('Frodo');
console.log(lotrChar.name); // To use the get or set functions, we can simply refer to their names as if it was a simple attribute
lotrChar.name = 'Gandalf'; // To use the get or set functions, we can simply refer to their names as if it was a simple attribute
console.log(lotrChar.name);
lotrChar._name = 'Sam'; // The _name attribute is not private, and we can still access it
console.log(lotrChar.name);
// To not acess a attribute, we can use the # symbol