-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClass-Inheritance.js
More file actions
83 lines (67 loc) · 2.73 KB
/
Class-Inheritance.js
File metadata and controls
83 lines (67 loc) · 2.73 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
'use strict'
/*
When ever we creates a Class , that means we are creating our own data type for some purpose. And It should be re-usable.
For making it re-usable, we have to make sure that we are not using any input/output operations in the class.
Class is always just a blue print of an Object that we need to create, and is like a 'Plan' for a 'House'.
*/
//node module for getting input values from CLI
var readline = require('readline');
//initializing read-line object
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
//A new Class/prototyping in js for creating an object with a member function and a member variable
var Man = function(name) {
//here this keyword is used to represent the 'this instance' of the class.
this.name = name;
this.sayHai = function() {
console.log('Hai I am ' + this.name);
};
};
/*Another class by inheriting the first class and implemented function overloading
you can see the sayHai function is overloaded here to implement some other datas to the out put.*/
var Student = function(name, className) {
/*this is how we inherit another class, in java we uses the 'extends' key word for this purpose.
here we sends 'this' object as first arguiment to the context of the parent class and then the 'Man' class will execute in same
memory location of the sub class 'Student'.
*/
Man.call(this, name);
//here we gives extra attributes that we need for the Student type objects
this.class = className;
this.sayHai = function() {
console.log('Hai I am ' + this.name + ' from ' + this.class);
};
};
rl.question("Are you a Student? (Y/N)", function(answer) {
newUser(answer, function(user) {
user.sayHai();
});
});
function newUser(answer, fn) {
switch (answer) {
case 'Y':
case 'y':
// reading input from CLI by asking a question
rl.question("What is your name?", function(name) {
// reading another input from CLI after the user gives answer to the first question
rl.question("Enter your batch and department:", function(className) {
/*
We uses 'new' keyword for creating an object of a class.
*/
var User = new Student(name, className);
rl.close();
fn(User); // This is a call back function passed when calling newUser function.
});
});
break;
case 'N':
case 'n':
rl.question("What is your name?", function(name) {
var User = new Man(name);
rl.close();
fn(User);
});
break;
}
}