Create new object instances using custom objects

Hi all

I’m studying web development online with little help from my tutor and I’m struggling.

I’m trying to Create new object instances using custom objects. When I launch the html file only the paragraph appears. Can someone please help. I’ve attached the code below.

<p id="dan1"></p> 
<script>
function car(make,model,colour,engine) {
this.make = make;
this.model = model;
this.colour = colour;
this.engine = engine;
}

var car1 = new car("Toyota", "Yaris", "Red"); "1.2");
var car2 = new car("Vauxhall", "Astra", "Grey", "1.5");
var car3 = new car("Ford", "Mondeo", "Blue", "1.2");
var car4 = new car("Mitsubishi", "Colt", "Black", "1.0");

document,getElementById("dan1").innerHTML =
"My car is a " + car1.make + " " + car1.model + ". My wifes car is a " +
car2.make + " " + car2.model. "My second car is a " + car3.make + " " + car3.model + ". "My wifes second car is a " + car4.make + " " + car4.model + ";

</script>

You’re not returning anything from your function. Try adding…

return this;

…as your the final line in your function/

Hi @danjoecollinson , there are a couple of syntax errors in your code:

  • There’s an excess closing parenthesis in the first car assignment
  • In the string concatenation part is quite over the place, there are unmatched string delimiters and missing + operators

You can see such errors in your browser console; I’d also suggest to use a code editor that highlights syntax problems right in the code.

Actually you don’t return anything from constructor functions… I’d suggest to adhere to the convention to start classes constructor functions with a capital letter though to avoid this kind of confusion.

1 Like

I really shouldn’t review code when I’ve only just opened my eyes, nor on an iPad for that matter.

Also notice that you have a comma rather than a period between document and getElementById

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.