Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function createAssemblyLine() {
- const decorators = {
- hasClima,
- hasAudio,
- hasParktronic,
- };
- function hasClima(obj) {
- obj.temp = 21;
- obj.tempSettings = 21;
- obj.adjustTemp = function () {
- if (obj.temp > obj.tempSettings) {
- obj.temp -= 1;
- } else if (obj.temp < obj.tempSettings) {
- obj.temp += 1;
- }
- };
- }
- function hasAudio(obj) {
- obj.currentTrack = null;
- obj.nowPlaying = function () {
- if (obj.currentTrack !== null) {
- console.log(
- `Now playing '${obj.currentTrack.name}' by ${obj.currentTrack.artist}`
- );
- }
- };
- }
- function hasParktronic(obj) {
- obj.checkDistance = function (distance) {
- let message = "";
- if (distance < 0.1) {
- message = "Beep! Beep! Beep!";
- } else if (distance < 0.25) {
- message = "Beep! Beep!";
- } else if (distance < 0.5) {
- message = "Beep!";
- }
- console.log(message);
- };
- }
- return decorators;
- }
- const assemblyLine = createAssemblyLine();
- const myCar = {
- make: "Toyota",
- model: "Avensis",
- };
- assemblyLine.hasClima(myCar);
- console.log(myCar.temp);
- myCar.tempSettings = 18;
- myCar.adjustTemp();
- console.log(myCar.temp);
- assemblyLine.hasAudio(myCar);
- myCar.currentTrack = {
- name: "Never Gonna Give You Up",
- artist: "Rick Astley",
- };
- myCar.nowPlaying();
- assemblyLine.hasParktronic(myCar);
- myCar.checkDistance(0.4);
- myCar.checkDistance(0.2);
- console.log(myCar);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement