-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypescript-oops-tutorial.html
More file actions
640 lines (537 loc) · 20.9 KB
/
Copy pathtypescript-oops-tutorial.html
File metadata and controls
640 lines (537 loc) · 20.9 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TypeScript OOPs Masterclass 🔥</title>
<!-- Google Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@300;400;500;600;700;800&family=JetBrains+Mono:wght@400;500&display=swap"
rel="stylesheet">
<!-- Highlight.js for Code Formatting -->
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/atom-one-dark.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
<style>
:root {
--bg-color: #0d1117; /* GitHub Darker */
--surface-color: #161b22;
--primary-color: #3178c6; /* TS Blue */
--primary-glow: rgba(49, 120, 198, 0.4);
--text-main: #f0f6fc;
--text-muted: #8b949e;
--border-color: rgba(255, 255, 255, 0.08);
--card-border: rgba(255, 255, 255, 0.1);
--accent-color: #f7df1e; /* JS Yellow */
--warning-color: #ff7b72; /* Reddish for errors/rules */
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
scroll-behavior: smooth;
}
body {
font-family: 'Plus Jakarta Sans', sans-serif;
background-color: var(--bg-color);
color: var(--text-main);
line-height: 1.6;
background-image:
radial-gradient(at 0% 0%, rgba(49, 120, 198, 0.15) 0px, transparent 50%),
radial-gradient(at 100% 100%, rgba(139, 148, 158, 0.1) 0px, transparent 50%);
background-attachment: fixed;
min-height: 100vh;
}
.layout {
display: flex;
gap: 2.5rem;
max-width: 1400px;
margin: 0 auto;
padding: 3rem 2rem;
align-items: flex-start;
}
.sidebar {
width: 280px;
flex-shrink: 0;
position: sticky;
top: 3rem;
max-height: calc(100vh - 6rem);
overflow-y: auto;
background: rgba(22, 27, 34, 0.8);
border: 1px solid var(--card-border);
border-radius: 16px;
padding: 1.5rem;
box-shadow: 0 20px 40px -20px rgba(0, 0, 0, 0.5);
backdrop-filter: blur(12px);
}
.sidebar h3 {
margin-top: 0;
margin-bottom: 1rem;
font-size: 1.1rem;
text-transform: uppercase;
letter-spacing: 1.5px;
color: var(--primary-color);
border-bottom: 1px solid var(--card-border);
padding-bottom: 1rem;
}
.sidebar ul {
list-style: none;
}
.sidebar li {
margin-bottom: 0.3rem;
}
.nav-section {
font-size: 0.85rem;
text-transform: uppercase;
letter-spacing: 1px;
color: var(--text-muted);
margin: 1.5rem 0 0.5rem 0;
padding-left: 0.5rem;
font-weight: 700;
}
.sidebar a {
text-decoration: none;
color: var(--text-muted);
display: block;
padding: 0.5rem 1rem;
border-radius: 8px;
transition: all 0.3s ease;
font-size: 0.95rem;
font-weight: 500;
}
.sidebar a:hover {
background: rgba(49, 120, 198, 0.15);
color: var(--primary-color);
transform: translateX(5px);
}
.main-content {
flex: 1;
min-width: 0;
}
header {
text-align: center;
margin-bottom: 4rem;
}
h1 {
font-size: 3rem;
font-weight: 800;
margin-bottom: 1rem;
background: linear-gradient(135deg, #3178c6 0%, #38bdf8 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-shadow: 0 0 40px var(--primary-glow);
}
.subtitle {
font-size: 1.2rem;
color: var(--text-muted);
}
.section {
background: rgba(22, 27, 34, 0.6);
border: 1px solid var(--card-border);
border-radius: 16px;
padding: 2.5rem;
margin-bottom: 2.5rem;
box-shadow: 0 20px 40px -20px rgba(0, 0, 0, 0.5);
backdrop-filter: blur(10px);
transition: transform 0.3s ease, box-shadow 0.3s ease;
scroll-margin-top: 3rem;
}
.section:hover {
transform: translateY(-5px);
box-shadow: 0 30px 60px -20px rgba(0, 0, 0, 0.6);
border-color: rgba(255, 255, 255, 0.15);
}
.divider {
text-align: center;
margin: 4rem 0;
padding: 2rem 0;
border-top: 1px dashed var(--card-border);
border-bottom: 1px dashed var(--card-border);
background: rgba(49, 120, 198, 0.05);
border-radius: 12px;
}
.divider h2 {
color: var(--primary-color);
margin: 0;
font-size: 2rem;
justify-content: center;
}
h2 {
font-size: 1.8rem;
margin-bottom: 1.5rem;
display: flex;
align-items: center;
gap: 0.5rem;
color: #fff;
}
.main-content h3 {
font-size: 1.3rem;
margin: 1.5rem 0 1rem;
color: #e2e8f0;
}
p {
margin-bottom: 1rem;
font-size: 1.1rem;
color: #c9d1d9;
}
/* Callouts */
.explanation {
background: rgba(49, 120, 198, 0.1);
border-left: 4px solid var(--primary-color);
padding: 1rem 1.5rem;
border-radius: 0 8px 8px 0;
margin: 1.5rem 0;
}
.concept-explanation {
background: rgba(247, 223, 30, 0.08); /* Yellowish JS vibe */
border-left: 4px solid var(--accent-color);
padding: 1rem 1.5rem;
border-radius: 0 8px 8px 0;
margin: 1.5rem 0;
}
.explanation p, .concept-explanation p {
margin-bottom: 0;
color: #e2e8f0;
}
pre {
background: #010409 !important;
border: 1px solid var(--border-color);
border-radius: 12px;
padding: 1.5rem !important;
margin: 1.5rem 0;
overflow-x: auto;
}
code {
font-family: 'JetBrains Mono', monospace !important;
font-size: 0.95rem;
}
/* Cards */
.rule-card {
background: linear-gradient(145deg, rgba(255, 123, 114, 0.1) 0%, rgba(220, 38, 38, 0.05) 100%);
border: 1px solid rgba(255, 123, 114, 0.2);
padding: 1.5rem;
border-radius: 12px;
margin-bottom: 1rem;
}
.rule-card h4 {
color: var(--warning-color);
margin-bottom: 0.5rem;
font-size: 1.2rem;
}
.tip-card {
background: linear-gradient(145deg, rgba(16, 185, 129, 0.1) 0%, rgba(5, 150, 105, 0.05) 100%);
border: 1px solid rgba(16, 185, 129, 0.2);
padding: 1.5rem;
border-radius: 12px;
margin-bottom: 1rem;
}
.tip-card h4 {
color: #34d399; /* Emerald */
margin-bottom: 0.5rem;
font-size: 1.2rem;
}
hr {
border: 0;
height: 1px;
background: var(--card-border);
margin: 2rem 0;
}
.cta-banner {
text-align: center;
background: linear-gradient(135deg, #3178c6 0%, #8b5cf6 100%);
padding: 3rem 2rem;
border-radius: 16px;
margin-top: 4rem;
}
.cta-banner h2 {
justify-content: center;
margin-bottom: 1rem;
}
.cta-banner p {
color: rgba(255, 255, 255, 0.9);
margin-bottom: 0;
}
.badge {
background: var(--primary-color);
color: #fff;
padding: 0.2rem 0.6rem;
border-radius: 6px;
font-size: 0.8rem;
font-weight: 700;
vertical-align: middle;
margin-left: 0.5rem;
}
.badge.yellow {
background: var(--accent-color);
color: #000;
}
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-track {
background: var(--bg-color);
}
::-webkit-scrollbar-thumb {
background: #30363d;
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: var(--primary-color);
}
/* Responsive */
@media (max-width: 900px) {
.layout {
flex-direction: column;
}
.sidebar {
width: 100%;
position: relative;
top: 0;
max-height: none;
margin-bottom: 2rem;
}
}
</style>
</head>
<body>
<div class="layout">
<!-- Sidebar Navigation -->
<aside class="sidebar">
<h3>📚 TS OOPs Guide</h3>
<nav>
<ul>
<li class="nav-section">Basics</li>
<li><a href="#intro-class">🏗️ Class & Object</a></li>
<li><a href="#constructor">🛠️ Constructor & `this`</a></li>
<li class="nav-section">Pillars of OOPs</li>
<li><a href="#encapsulation">🔒 Encapsulation (Access Modifiers)</a></li>
<li><a href="#inheritance">🧬 Inheritance</a></li>
<li><a href="#polymorphism">🎭 Polymorphism</a></li>
<li><a href="#abstraction">👻 Abstraction</a></li>
<li class="nav-section">Pro Features</li>
<li><a href="#readonly-getter">👀 Readonly & Getters/Setters</a></li>
<li><a href="#interfaces">📝 Interfaces (Contracts)</a></li>
<li><a href="#static">⚡ Static Properties</a></li>
</ul>
</nav>
</aside>
<!-- Main Content area -->
<main class="main-content">
<header>
<h1>TypeScript OOPs Masterclass 🔥</h1>
<p class="subtitle">Classes, Interfaces, aur Object-Oriented TS ka Poora Nuskha 🧑💻</p>
</header>
<!-- ==========================================
BASICS
=========================================== -->
<div class="section" id="intro-class">
<h2>🏗️ 1. Class & Object (The Blueprints)</h2>
<p><strong>Class:</strong> Ek naksha (blueprint) hota hai. (Jaise "Car" banane ka map).<br>
<strong>Object:</strong> Us map se bani hui actual cheez! (Jaise tera "Swift" model).</p>
<pre><code class="language-typescript">class Car {
brand: string; // Properties
model: string;
startEngine(): void { // Methods
console.log("Vroom Vroom! 🏎️");
}
}
// Object banana (Instantiate)
const myCar = new Car();
myCar.brand = "Toyota";
myCar.startEngine();</code></pre>
</div>
<div class="section" id="constructor">
<h2>🛠️ 2. Constructor & `this` Keyword</h2>
<p>Jab bhi Object banta hai, pehli baar koi cheez initialize karni ho, toh <code>constructor</code> chalata hai. <code>this</code> current object ko point karta hai.</p>
<pre><code class="language-typescript">class User {
name: string;
age: number;
constructor(userName: string, userAge: number) {
this.name = userName;
this.age = userAge;
console.log(`${this.name} paida ho gya bhai! 🍼`);
}
}
// Yahan automatically constructor chalega
const rohan = new User("Rohan", 25); </code></pre>
<div class="tip-card">
<h4>💡 TS Shortcut (Parameter Properties)</h4>
<p>TS me properties upar likhne ki zarurat nahi. Constructor me direct access modifier daal de, TS khud handle kar lega:</p>
<pre style="margin-top: 10px; background: rgba(0,0,0,0.5) !important"><code class="language-typescript">class User {
constructor(public name: string, public age: number) {}
}</code></pre>
</div>
</div>
<!-- ==========================================
PILLARS
=========================================== -->
<div class="divider" id="encapsulation">
<h2>🔒 ENCAPSULATION (Data Security)</h2>
<p style="margin-top: 10px; color: var(--text-muted)">Koi bahar wala teri class ka data chhed na paaye!</p>
</div>
<div class="section">
<h2>🛡️ 3. Access Modifiers</h2>
<p>TypeScript mein 3 levels ki security/access hote hain:</p>
<ul>
<li><strong style="color: #34d399">public</strong> (Default): Koi bhi kahin se bhi access kar le.</li>
<li><strong style="color: #fca5a5">private</strong>: Sirf class ke andar hi access hoga. (Bank Balance).</li>
<li><strong style="color: #fcd34d">protected</strong>: Class ke andar + uske baccho (Inherited classes) me access hoga.</li>
</ul>
<pre><code class="language-typescript">class BankAccount {
public accountName: string;
private balance: number; // Bahar se access nahi hoga!
protected pinCode: number; // Sirf child class padh sakti hai
constructor(name: string, amt: number) {
this.accountName = name;
this.balance = amt;
}
showBalance() {
console.log(`Balance: $${this.balance}`); // Class ke andar allowed hai!
}
}
const myAc = new BankAccount("Rohan", 5000);
// myAc.balance = 0; // 🛑 ERROR: Property 'balance' is private!</code></pre>
</div>
<div class="divider" id="inheritance">
<h2>🧬 INHERITANCE (Baap - Beta Relation)</h2>
</div>
<div class="section">
<h2>👶 4. Extending Classes (`extends`)</h2>
<p>Code bar-bar likhne se bachne ke liye, ek class dusri Parent class ki properties "Inherit" (<kbd>extends</kbd>) karti hai.</p>
<pre><code class="language-typescript">class Animal { // Parent Class
constructor(public name: string) {}
eat() { console.log(`${this.name} khana kha rha hai! 🍖`); }
}
class Dog extends Animal { // Child Class
breed: string;
constructor(name: string, breed: string) {
super(name); // 'super' se parent ka constructor call hota hai (Zaruri h!)
this.breed = breed;
}
bark() { console.log("Bhow Bhow! 🐕"); }
}
const tommy = new Dog("Tommy", "Pug");
tommy.eat(); // Parent wala method kaam aa gaya!
tommy.bark(); // Bachhe ka apna method</code></pre>
<div class="rule-card">
<h4>✅ Rule of Inheritance:</h4>
<p>Agar tu child class me <code>constructor()</code> banata hai, toh pehli line me <code>super()</code> call karna <strong>Compulsory</strong> hai.</p>
</div>
</div>
<div class="divider" id="polymorphism">
<h2>🎭 POLYMORPHISM (Behrupiya)</h2>
</div>
<div class="section">
<h2>🔄 5. Method Overriding</h2>
<p>Jab Bachha (Child class), Baap (Parent class) ke same method ko apne style me <strong>re-define</strong> karta hai.</p>
<pre><code class="language-typescript">class Employee {
work() { console.log("Kaam kar raha hu..."); }
}
class Manager extends Employee {
// Same method name, different behavior!
work() {
console.log("Employees par chilla raha hu! 🗣️");
}
}
const mgr = new Manager();
mgr.work(); // Result: "Employees par chilla raha hu! 🗣️"</code></pre>
</div>
<div class="divider" id="abstraction">
<h2>👻 ABSTRACTION (Details Chupana)</h2>
</div>
<div class="section">
<h2>🌫️ 6. Abstract Classes</h2>
<p>Aisi class jiska koi Direct Object nahi ban skta (Aadhi adhuri class). Ye dusrson ke liye sirf "Rulebook" hoti hai ki kya-kya fulfill karna hai.</p>
<pre><code class="language-typescript">abstract class Shape {
abstract calculateArea(): number; // Rule: Har shape ko ye method dena hi pdega!
printDetails() {
console.log("Main ek basic shape hu!"); // Concrete method (jo override zaruri nahi)
}
}
// const s = new Shape(); // 🛑 ERROR: Cannot create an instance of abstract class
class Circle extends Shape {
constructor(public radius: number) { super(); }
// Majboori hai method implement karna:
calculateArea(): number {
return Math.PI * this.radius * this.radius;
}
}</code></pre>
</div>
<!-- ==========================================
PRO FEATURES
=========================================== -->
<div class="divider" id="readonly-getter">
<h2>💎 PRO FEATURES (TypeScript Magic)</h2>
</div>
<div class="section">
<h2>👀 7. Readonly, Getters & Setters</h2>
<p><strong><kbd>readonly</kbd>:</strong> Ek baar assign ho gaya, wapas change nahi hoga! (e.g. Aadhar Number).<br>
<strong><kbd>get</kbd> & <kbd>set</kbd>:</strong> Custom logic run karna job bhi user property padhna ya set karna chahe.</p>
<pre><code class="language-typescript">class UserProfile {
readonly id: string = "USR_123";
private _age: number = 18; // Convention: private variables start with "_"
// GETTER: As a property padh sakte ho (function ki tarah call () ni karna pdta!)
get myAge(): number {
return this._age;
}
// SETTER: Value set karte time logic chala sakte hain
set changeAge(newAge: number) {
if(newAge < 0) throw new Error("Age minus me ni ho skti be!");
this._age = newAge;
}
}
const u = new UserProfile();
// u.id = "44"; // 🛑 ERROR: Cannot assign to 'id' because it is a read-only property.
u.changeAge = 25; // Setter automatically trigger hoga (bina usko function ki tarah call kiye)
console.log(u.myAge); // Getter trigger! (Output: 25)</code></pre>
</div>
<div class="section" id="interfaces">
<h2>📝 8. Interfaces (Data Contracts)</h2>
<p>TypeScript mein Classes ek object ko structure dene ke liye <code>implements</code> keyword ka use kar sakti hain. Matlab ek guarantee contract sign karna padta hai.</p>
<pre><code class="language-typescript">// Contract Document
interface Driveable {
brand: string;
start(): void;
}
// Class ko shart manni pdegi
class Truck implements Driveable {
brand = "Tata";
start() {
console.log("Truck Start Hua, dhuk dhuk dhuk!");
}
}</code></pre>
<div class="concept-explanation">
<p>👉 <strong>Abstract Class vs Interface:</strong> Abstract class me tu logic + rules dono de sakta hai. Interface me sirf "Rules" rehte hain, logic nahi.</p>
</div>
</div>
<div class="section" id="static">
<h2>⚡ 9. Static Properties & Methods</h2>
<p>Normal properties Class <strong>ke object</strong> par lagti hain (`const x = new Math; x.calculate()`).<br>
<strong><kbd>static</kbd></strong> properties <strong>Direct Class</strong> par lagti hain (Bina object banaye!).</p>
<pre><code class="language-typescript">class DatabaseConf {
// Bina 'new DatabaseConf()' chalaye kaam karega:
static url: string = "mongodb://localhost:27017";
static connect() {
console.log(`Connecting to ${this.url}...`);
}
}
// See? Sidha Class Name se bula rhe hain:
console.log(DatabaseConf.url);
DatabaseConf.connect();</code></pre>
</div>
<div class="cta-banner">
<h2>TS OOPs Mastery Completed! 🎉</h2>
<p>Ab tu Java/Angular developers ke sath aaram se level match karke baat kar sakta hai. Ab classes deke thara bhai darne wala nahi hai! <br><br>
<strong>Keep Crushing The Code! 💻🔥</strong>
</p>
</div>
</main>
</div>
</body>
</html>