-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance_using_classes.js
More file actions
91 lines (67 loc) · 2.32 KB
/
Copy pathinheritance_using_classes.js
File metadata and controls
91 lines (67 loc) · 2.32 KB
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
"use strict";
// Inheritance between ES6 Classes:
// To implement inheritance b/w ES6 classes we need two ingredients
// 1. Extend Keyword:
// To link prototypes behind the scenes
// Inside the the class we still need the constructor function
// 2. Super() Function :
// Its basically the constructor function of the parent class, but the idea is still same
// Instead of doing Person.call() manually, we use Super() to set this keyword and parent class properties
// Parent Class:
class Person {
//Properties:
constructor(birthYear, firstName) {
this.birthYear = birthYear;
this.firstName = firstName;
}
//Methods
calcAge() {
return 2025 - this.birthYear;
}
greet() {
console.log(`Greetings from ${this.firstName}`);
}
}
// Student Class : Child Class
// We Need:
// Extend Keyword.
// Super(): This must be called inside constructor() of child class, and before setting any property in child class, because it sets this keyword
class Student extends Person {
//Setting properties:
constructor(birthYear, firstName, course) {
//Super Function Call:
super(birthYear, firstName);
// Extended property:
this.course = course;
}
//Methods:
introduce() {
console.log(`Hello my name is ${this.firstName}`);
}
}
const ali = new Student(2001, "Ali", "CS");
ali;
console.log(ali.calcAge());
ali.greet();
ali.introduce();
console.log(ali instanceof Student);
console.log(ali instanceof Person);
// So again here we can see, Its not the class that is the prototype property of instance
// So the idea of prototypal inheritance is still just the same
console.log(Student.isPrototypeOf(ali));
console.log(Person.isPrototypeOf(ali));
// The prototype property of Both Classes is the prototype of instance
console.log(Student.prototype.isPrototypeOf(ali));
console.log(Person.prototype.isPrototypeOf(ali));
// Now:
// JavaScript Does not have:
// Access specifiers: Public Private and Protected Keywords
// So to implement Encapsulation we use some conventions
// we use # symbol before any property to mark it private for example
// #password
// And same goes for private methods:
// #functionName(){}
// And to access them inside class we still do:
// this.#password
// And if we have a property name , and we want a method with same name
// we can use _ symbol to keep both at same time with no conflict