-2

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);

2
  • 1
    as @Blaze349 states, you missed to add a + sign for string concatenation. I suggest you to use some kind of editor that supports syntax checking, since that will help you to find such errors faster. It seems that you are at an early stage of developing, hence an editor with syntax checkin will also help you to get used to the syntax faster. When you are "good enough" in writing code, you may switch back to a plain editor, since you know the syntax and make less mistakes. :) - Keep on! Commented Apr 26, 2017 at 5:47
  • Thanks man! Im using Sublime so Im guessing something like JSLint would be useful for me. Commented Apr 26, 2017 at 7:11

2 Answers 2

-1

Fixed it.

The problem is here:

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

You need to add a + sign between "speed of" and powerCar.speed.

Sign up to request clarification or add additional context in comments.

1 Comment

Majority of the time is something stupid that holds me up!
-2

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.