Search code examples
javascriptobjectconstructor

basic constructor object help - JS


Trying to learn Objects in JS and having a bit of trouble trying to get this to work. Its just a simple Object Constructor but seems to be the bane of my life today. Unfortunately I dont have anyone at hand to turn to that can help me out. Also if anyone knows of good tutorials with real life working examples instead of the just how functions, loops etc work then Id really appreciate it.

function car(model, doors, color, speed){

this.model = model;
this.doors = doors;
this.color = color;
this.speed = speed;

}

var powerCar = new car ("M3", "4 door", "phoenix", "220pmh");

console.log("This " + powerCar.model + "has " + powerCar.doors + "has a top speed of " powerCar.speed);


Solution

  • you can also traverse all the properties by for loop like below:

    for (var prop in powerCar) {
        console.log(prop + ': '+powerCar[prop])
    }