Advertisement
stanevplamen

JS OOP Class

Sep 21st, 2022
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.54 KB | Software | 0 0
  1. var Course = (function () {
  2.  
  3.     // hidden data only for Course
  4.     function calculateTotalScoreFor(student) {
  5.         var result = this._formula(student);
  6.         return result;
  7.     }
  8.  
  9.     // creating private method to reduse code repeating
  10.     function returnTopStudentsSortedBy(sortBy, count) {
  11.         if (!this._totalScores || this._totalScores.length !== this._students.length) {
  12.             this.calculateResults();
  13.         }
  14.  
  15.         count = count || this._totalScores.length;
  16.         var totalScores = this._totalScores.slice(0); // copy of the array
  17.         totalScores.sort(sortBy);
  18.  
  19.         // return totalScores.slice(0, count); // returning exact number -> count
  20.         // till now the functions returs objects student, total score
  21.         // making map function
  22.         return totalScores.map(function (student) {
  23.             //var studentToReturn = student.student;
  24.             // for being other referation
  25.             var studentToReturn = Object.create(student.student); // making a copy
  26.             studentToReturn.totalScore = student.totalScore;
  27.             return studentToReturn;
  28.         }).slice(0, count);
  29.  
  30.         // the other version is to return new object
  31.         //return {
  32.         //    name: student.student.name
  33.         //}
  34.     }
  35.  
  36.  
  37.     // defining the different sorting functions
  38.     function sortByExam(st1, st2) {
  39.         // descending sort
  40.         return st2.student.exam - st1.student.exam;
  41.         // ascending sort
  42.         // return st1.student.exam - st2.student.exam;
  43.     }
  44.     function sortByTotalScore(st1, st2) {
  45.         return st2.totalScore - st1.totalScore;
  46.     }
  47.  
  48.     // function constructor
  49.     function Course(title, formula) {
  50.         this._students = [];
  51.         this._title = title;
  52.         this._formula = formula;
  53.     }
  54.  
  55.     //Course.prototype.newTestFunc = function () {
  56.     //    return this._title + " New";
  57.     //}
  58.  
  59.     Course.prototype = {
  60.         addStudent: function (student) {
  61.             if (!(student instanceof Student)) {
  62.                 throw {
  63.                     message: 'Trying to add an objects that is not an instance of Student'
  64.                 }
  65.             }
  66.             this._students.push(student);
  67.             // returting the same function to allow chaining
  68.             return this;
  69.         },
  70.         calculateResults: function () {
  71.  
  72.             var i, len, student, studentTotalScore;
  73.  
  74.             this._totalScores = [];
  75.             len = this._students.length;
  76.             for (i = 0; i < len; i++) {
  77.                 student = this._students[i];
  78.                 // public methods
  79.                 //studentTotalScore = this.calculateTotalScoreFor(student);
  80.                 // making the method private
  81.                 studentTotalScore = calculateTotalScoreFor.call(this, student);
  82.  
  83.                 this._totalScores.push({
  84.                     student: student,
  85.                     totalScore: studentTotalScore
  86.                 });
  87.             }
  88.             return this;
  89.         },
  90.         getTopStudentsByExam: function (count) {
  91.  
  92.             return returnTopStudentsSortedBy.call(this, sortByExam, count);
  93.         },
  94.         getTopStudentsByTotalScore: function (count) {
  95.  
  96.             return returnTopStudentsSortedBy.call(this, sortByTotalScore, count);
  97.         },
  98.         testCourse: function () {
  99.             return this._title;
  100.         }
  101.  
  102.     }
  103.  
  104.     // other way to create methods
  105.     //Course.prototype.addStudent = function (student) { };
  106.  
  107.     return Course;
  108.  
  109.     //return {
  110.     //    Course: Course
  111.     //}
  112.  
  113. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement