简体   繁体   English

Javascript返回对象中的函数将不会显示警告框

[英]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. 所以基本上我有这个代码,它在函数中有一个返回值,然后应该显示返回的变量“Price”,但警告框不显示。 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,对象属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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