DEV Community

Zayno
Zayno

Posted on

A dynamic way of accessing a obect in Javascript

hello all,
I have this code from a book i'm reading and i want to spice up the exercise by adding a dynamic touch to it.My aim is to access the properties of the object without explicitly assinging the object taxi.make and taxi.model what i want is to give it a dynamic property that whenever i change the object e.g. limosine i dont have to write limousine.make or limousine.model.

here is the code

function init() {
  let lambo ={
    make:'KIA',
    model:'Picanto',
    year:1999,
    color:'beige',
    passenger:4,
    convertible:false,
    mileage:2000,
    engine:'gdi',
    transmission:'manual',
    fuel_type:'gasoline'
  }
function prequal(car) {
  if (car.mileage > 10000) {
    return false;
  }else if (car.year >= 2000) {
    return false;
  } 
  return true;
}    
let worthalook = prequal (lambo);
if (worthalook) {
  document.write('You gotta check this ' + lambo.make + ' ' + lambo.model);
}else{
  document.write('You should really pass on the ' + lambo.make + ' ' + lambo.model);
}
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)