-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor-function-basics.js
More file actions
40 lines (35 loc) · 1.13 KB
/
Copy pathconstructor-function-basics.js
File metadata and controls
40 lines (35 loc) · 1.13 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
/**
* @Author Takudzwa Frank Mukarakate
* @Version 1.0.0
*
* Introduction to JS constructor functions similar to classes in OOP
*
*/
function Person(first_name, middle_name, last_name, date_of_birth = new Date()) {
this.first_name = first_name;
this.middle_name = middle_name;
this.last_name = last_name;
this.date_of_birth = date_of_birth;
}
/**
* Constructor function for a school subject
* @param name Name of the subject
* @param code identifying code of the subject
* @param teacher person teaching the subject
*/
function Subject(name, code, teacher) {
this.name = name;
this.code = code;
this.teacher = teacher;
this.students = [];
}
// Dummy data
let teacher = new Person("Teacher", "Teach", "McTeacherson");
let intoJavaScriptSubject = new Subject("Into to JavaScript", "JS101", teacher);
// Creating and populating students
for (let i = 0; i < 5; i++) {
let currentStudent = new Person("Student" + (i + 1), "Stu" + (i + 1), "Stud" + (i + 1));
intoJavaScriptSubject.students.push(currentStudent); // Remember push and pop not add/append
}
// Display object
console.log(intoJavaScriptSubject);