Your issue is that object
needs to be capitalized - object
isn't a thing in JavaScript, but Object
is.
You want:
var car=new Object();
As w3schools says, JavaScript identifiers are case sensitive:
All JavaScript identifiers are case sensitive.
The variables lastName
and lastname
, are two different variables.
So object
and Object
are two different things, and you want Object
- almost everything in JS starts as an Object
.
More on Object here.
Also, as ozil pointed out, you should change car.speed()
to simply car.speed
. You've previously set car.speed
to 220
, so it's not a function. car.speed()
tries to treat it as a function, which will cause issues.
So, in all, this code is what you want:
var car=new Object();
car.name="Mercedes Benz";
car.speed=220;
car.showNameAndSpeed=function(){
console.log("The name of the car is " + car.name + " and the topspeed is " + car.speed);
};
car.showNameAndSpeed();
car.speed()
is incorrect.speed
is a property of thecar
object, it's not a function.var car=new Object();
var car = {};
does the trick (or use capitalO
in your code). Notice also, thatspeed
is not a function.