daily pastebin goal
72%
SHARE
TWEET

Untitled

a guest May 19th, 2017 41 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Listing 1-1. Object-Oriented JavaScript Representing a Lecture and a Schedule of Lectures
  2. // The constructor for our 'Lecture'
  3. // Takes two strings, name and teacher
  4. function Lecture( name, teacher ) {
  5.     // Save them as local properties of the object
  6.     this.name = name;
  7.     this.teacher = teacher;
  8. }
  9.  
  10. // A method of the Lecture class, used to generate
  11. // a string that can be used to display Lecture information
  12. Lecture.prototype.display = function(){
  13.     return this.teacher + " is teaching " + this.name;
  14. };
  15.  
  16. // A Schedule constructor that takes in an
  17. // array of lectures
  18. function Schedule( lectures ) {
  19.     this.lectures = lectures;
  20. }
  21.  
  22. // A method for constructing a string representing
  23. // a Schedule of Lectures
  24. Schedule.prototype.display = function(){
  25.     var str = "";
  26.    
  27.     // Go through each of the lectures, building up
  28.     // a string of information
  29.     for ( var i = 0; i < this.lectures.length; i++ )
  30.         str += this.lectures[i].display() + " ";
  31.     return str;
  32. };
RAW Paste Data
Top