|
| 1 | +function createAssemblyLine() { |
| 2 | + let myObject = { |
| 3 | + hasClima: (inputObject) => { |
| 4 | + inputObject.temp = 21; //default |
| 5 | + inputObject.tempSettings = 21; //default |
| 6 | + inputObject.adjustTemp = function () { |
| 7 | + if (this.temp < this.tempSettings) { |
| 8 | + this.temp += 1; |
| 9 | + } else if (this.temp > this.tempSettings) { |
| 10 | + this.temp -= 1; |
| 11 | + } |
| 12 | + } |
| 13 | + }, |
| 14 | + |
| 15 | + hasAudio: (inputObject) => { |
| 16 | + inputObject.currentTrack = null; // default |
| 17 | + inputObject.nowPlaying = function () { |
| 18 | + if (this.currentTrack !== null) { |
| 19 | + console.log(`Now playing '${this.currentTrack.name}' by ${this.currentTrack.artist}`); |
| 20 | + } |
| 21 | + } |
| 22 | + }, |
| 23 | + |
| 24 | + hasParktronic: (inputObject) => { |
| 25 | + inputObject.checkDistance = (distance) => { |
| 26 | + if (distance < 0.1) { |
| 27 | + console.log('Beep! Beep! Beep!'); |
| 28 | + } else if (distance >= 0.1 && distance < 0.25) { |
| 29 | + console.log('Beep! Beep!'); |
| 30 | + } else if (distance >= 0.25 && distance < 0.5) { |
| 31 | + console.log('Beep!'); |
| 32 | + } else { |
| 33 | + console.log(''); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + } |
| 39 | + return myObject; |
| 40 | +} // 100/100 ... now playing neva gonna give u up |
| 41 | +// lambda functions dont work with 'this.something' !!!! |
| 42 | +const assemblyLine = createAssemblyLine(); |
| 43 | + |
| 44 | +const myCar = { |
| 45 | + make: 'Toyota', |
| 46 | + model: 'Avensis' |
| 47 | +}; |
| 48 | + |
| 49 | +assemblyLine.hasClima(myCar); |
| 50 | +console.log(myCar.temp); |
| 51 | +myCar.tempSettings = 18; |
| 52 | +myCar.adjustTemp(); |
| 53 | +console.log(myCar.temp); |
| 54 | + |
| 55 | +assemblyLine.hasAudio(myCar); |
| 56 | +myCar.currentTrack = { |
| 57 | + name: 'Never Gonna Give You Up', |
| 58 | + artist: 'Rick Astley' |
| 59 | +}; |
| 60 | +myCar.nowPlaying(); |
| 61 | + |
| 62 | +assemblyLine.hasParktronic(myCar); |
| 63 | +myCar.checkDistance(0.4); |
| 64 | +myCar.checkDistance(0.2); //- seems to work?? |
| 65 | + |
| 66 | +console.log(myCar); |
| 67 | + |
| 68 | + |
0 commit comments