So basically i have this code which has a return in a function which should then display the returned variable "Price" but the alert box does not show. If i delete all code except alert it will. I can't find any misspelled words or pieces of code. Could you help me ?
<html>
<body>
<script>
var auto = {
merk: 'BMW',
model: 1,
aantal deuren: 5,
bouwjaar: 1990,
prijs : 20000,
price: function(){
return this.prijs;
}
};
var x = auto.price();
alert(x);
</script>
</body>
You have invalid property name in the object:
aantal deuren: 5,
If the property name is not valid identifier you need to wrap it in quotes:
var auto = {
merk: 'BMW',
model: 1,
"aantal deuren": 5,
bouwjaar: 1990,
prijs: 20000,
price: function () {
return this.prijs;
}
};
var x = auto.price();
alert("prijs");
Use '' for 'aantal deuren': 5, or remove space from aantaldeuren: 5, object property
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With