Created
September 9, 2017 10:24
-
-
Save bosr/01a5cf4bc140a65987317dc7dbd1cea8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Teacher = function (data) { | |
this.name = data.name; | |
this.greet = function (studentCnt) { | |
let promise = new Promise(function (resolve, reject) { | |
if (studentCnt === 0) { | |
reject('Zero students in class'); | |
} else { | |
resolve(`Hello to ${studentCnt} of my favorite students!`); | |
} | |
}); | |
return promise; | |
} | |
} | |
var Classroom = function (data) { | |
this.subject = data.name; | |
this.teacher = data.teacher; | |
this.students = []; | |
this.addStudent = function (data) { | |
this.students.push(data); | |
this.greet(); | |
} | |
this.greet = () => { | |
this.teacher.greet(this.students.length).then( | |
greeting => console.log(`${this.teacher.name} says: `, greeting), | |
err => console.log(err) | |
); | |
} | |
} | |
var myTeacher = new Teacher({ name: 'James' }); | |
var myClassroom = new Classroom({ name: 'The Future of JavaScript', teacher: myTeacher }); | |
myClassroom.addStudent({ name: 'Dave' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment