Advertisement
viligen

assemblyLine

May 26th, 2022
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createAssemblyLine() {
  2.     const decorators = {
  3.         hasClima,
  4.         hasAudio,
  5.         hasParktronic,
  6.     };
  7.  
  8.     function hasClima(obj) {
  9.         obj.temp = 21;
  10.         obj.tempSettings = 21;
  11.         obj.adjustTemp = function () {
  12.             if (obj.temp > obj.tempSettings) {
  13.                 obj.temp -= 1;
  14.             } else if (obj.temp < obj.tempSettings) {
  15.                 obj.temp += 1;
  16.             }
  17.         };
  18.     }
  19.  
  20.     function hasAudio(obj) {
  21.         obj.currentTrack = null;
  22.         obj.nowPlaying = function () {
  23.             if (obj.currentTrack !== null) {
  24.                 console.log(
  25.                     `Now playing '${obj.currentTrack.name}' by ${obj.currentTrack.artist}`
  26.                 );
  27.             }
  28.         };
  29.     }
  30.     function hasParktronic(obj) {
  31.         obj.checkDistance = function (distance) {
  32.             let message = "";
  33.             if (distance < 0.1) {
  34.                 message = "Beep! Beep! Beep!";
  35.             } else if (distance < 0.25) {
  36.                 message = "Beep! Beep!";
  37.             } else if (distance < 0.5) {
  38.                 message = "Beep!";
  39.             }
  40.             console.log(message);
  41.         };
  42.     }
  43.  
  44.     return decorators;
  45. }
  46.  
  47. const assemblyLine = createAssemblyLine();
  48.  
  49. const myCar = {
  50.     make: "Toyota",
  51.     model: "Avensis",
  52. };
  53.  
  54. assemblyLine.hasClima(myCar);
  55. console.log(myCar.temp);
  56. myCar.tempSettings = 18;
  57. myCar.adjustTemp();
  58. console.log(myCar.temp);
  59.  
  60. assemblyLine.hasAudio(myCar);
  61. myCar.currentTrack = {
  62.     name: "Never Gonna Give You Up",
  63.     artist: "Rick Astley",
  64. };
  65. myCar.nowPlaying();
  66.  
  67. assemblyLine.hasParktronic(myCar);
  68. myCar.checkDistance(0.4);
  69. myCar.checkDistance(0.2);
  70.  
  71. console.log(myCar);
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement