Advertisement
Pijomir

Cars

Jan 29th, 2024
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createCarsObjects(input) {
  2.     let carsCollection = {};
  3.     let carsCreator = {
  4.         create: (n) => carsCollection[n] = {},
  5.         createAndInherits: (n, parrentN) => carsCollection[n] = Object.create(carsCollection[parrentN]),
  6.         set: (n, key, value) => carsCollection[n][key] = value,
  7.         print: (n) => {
  8.             let result = [];
  9.             for (let key in carsCollection[n]) {
  10.                 result.push(`${key}:${carsCollection[n][key]}`);
  11.             }
  12.  
  13.             console.log(result.join(','));
  14.         }
  15.     }
  16.  
  17.     input.forEach(a => {
  18.         let [command, car, otherParam1, otherParam2] = a.split(' ');
  19.         if (command === 'create') {
  20.             otherParam1 === 'inherit' ? carsCreator.createAndInherits(car, otherParam2) : carsCreator.create(car);
  21.         } else if (command === 'set') {
  22.             carsCreator.set(car, otherParam1, otherParam2);
  23.         } else {
  24.             carsCreator.print(car);
  25.         }
  26.     });
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement