简体   繁体   中英

Javascript return in function in object won't show alert box

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

使用''代表'aantal deuren': 5,或从aantaldeuren: 5,删除空格aantaldeuren: 5,对象属性

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM