Skip to content

Commit 539eb63

Browse files
committed
OOP(Basics)
1 parent 5739e57 commit 539eb63

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

10. OOP(Basics)/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<h1 id="showMessage"></h1>
10+
<!-- link javacript file -->
11+
<script src="js/script.js"></script>
12+
13+
</body>
14+
</html>

10. OOP(Basics)/js/script.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// JavaScript Classes are templates for JavaScript Objects.
2+
// ECMAScript 2015, also known as ES6, introduced JavaScript Classes.
3+
4+
// JavaScript Class Syntax
5+
// Use the keyword class to create a class.
6+
// Always add a method named constructor():
7+
8+
// class ClassName {
9+
// constructor() { ... }
10+
// }
11+
12+
class Car{
13+
constructor(car, year) {
14+
this.car = car;
15+
this.year = year;
16+
}
17+
age(){
18+
const currentYear = new Date().getFullYear();
19+
return currentYear - this.year;
20+
}
21+
}
22+
23+
let car1 = new Car("Ford", 2014);
24+
let car2 = new Car("Audi", 2019);
25+
26+
console.log(car1.car);
27+
console.log(car1.year);
28+
console.log(car2.car);
29+
console.log(car2.year);
30+
31+
32+
console.log(car1.age());
33+
34+
35+
class Person{
36+
constructor(name, village, bdate) {
37+
this.name = name;
38+
this.village =village;
39+
this.bdate = bdate;
40+
}
41+
42+
calculateAge(){
43+
let birthdate = new Date(this.bdate);
44+
let difftime = Date.now() - birthdate.getTime();
45+
let ageDate = new Date(difftime);
46+
return Math.abs(ageDate.getUTCFullYear() -1970);
47+
}
48+
}
49+
50+
let person1 = new Person("Aktauzzaman","Cox's Bazar", "04-13-1998");
51+
console.log(person1.bdate);
52+
console.log(person1.name);
53+
console.log(person1.calculateAge());
54+
55+
56+
console.log("Examine: ");
57+
let bdate = "04-13-1998";
58+
let birthdate = new Date(bdate);
59+
60+
let difftime = Date.now() - birthdate.getTime();
61+
//If assume current year is Sep, 2024. 1970 to 2024 = > 54 years
62+
// If assume birthdate is Aprinl, 1998. 1970 to 1998 => 28 years
63+
// 54-28 = 26 years
64+
let ageDate = new Date(difftime); // this function add 26 years equal ms
65+
// to 1970 that will be = 1996
66+
console.log(Math.abs(ageDate.getUTCFullYear() -1970)); // 1996-1970 = 26
67+
68+
69+
// JavaScript Class Inheritance
70+
// To create a class inheritance, use the extends keyword.
71+
// A class created with a class inheritance inherits all the methods from another class:
72+
73+
class Person1{
74+
constructor(fname, lname){
75+
this.fname = fname;
76+
this.lname = lname;
77+
}
78+
79+
getFullName(){
80+
return this.fname + ' ' + this.lname;
81+
}
82+
83+
greet(){
84+
console.log(`Hello, ${this.getFullName()}`);
85+
}
86+
}
87+
88+
89+
let p1 = new Person1("Abdullah", "Abdur Rahman");
90+
p1.greet();
91+
92+
class Customer extends Person1{
93+
constructor(fname, lname, cusId, address){
94+
super(fname,lname);
95+
this.cusId = cusId;
96+
this.address = address;
97+
}
98+
getCustInfo(){
99+
console.log("\tCustomer Ifo:");
100+
console.log("Name:", this.getFullName());
101+
console.log("Customer Id:", this.cusId);
102+
console.log("Address:", this.address);
103+
}
104+
}
105+
106+
107+
p2 = new Customer("Akib", "Zabed", 123, "Dhaka");
108+
p2.getCustInfo();
109+
110+
// The super() method refers to the parent class.
111+
// By calling the super() method in the constructor method, we call the parent's constructor method and gets access to the parent's properties and methods.
112+
113+
// Unlike functions, and other JavaScript declarations, class declarations are not hoisted.
114+
// That means that you must declare a class before you can use it:
115+
116+
// const myCar = new Car1("Ford") //cause error
117+
118+
class Car1 {
119+
constructor(brand) {
120+
this.carname = brand;
121+
}
122+
}
123+
124+
//Now you can use the class:
125+
const myCar = new Car1("Ford")
126+
console.log(myCar.carname);
127+
128+
// Static Function
129+
130+
// Static class methods are defined on the class itself.
131+
// You cannot call a static method on an object, only on an object class.
132+
class P{
133+
constructor(fname,lname) {
134+
this.fname = fname;
135+
this.lname = lname;
136+
}
137+
static hello() {
138+
return "Hello!!";
139+
}
140+
141+
getFullName(){
142+
return this.fname + ' ' + this.lname;
143+
}
144+
}
145+
146+
let ob1 = new P("Abdullah", "Abdur Rahman");
147+
console.log(P.hello());
148+
console.log(ob1.getFullName());
149+
// ob1.hello();//Error

javascript book impatient.pdf

2.54 MB
Binary file not shown.

0 commit comments

Comments
 (0)