为什么`name`变量不会出错?

时间:2017-03-10 04:34:36

标签: javascript html

当我尝试在js中创建和访问对象的属性时,它让我混淆了未定义的变量:namename_aaa。有关更多信息,请参阅以下代码:

<!DOCTYPE html>
<html>
<body>

<p id="demo">Display the result here.</p>

<script>
    var persion = {
        name: "GSP",
        age: 50,
        show: function() {
            document.getElementById("demo").innerHTML = "Hello " + this.name;
            /*
                Output: Hello GSP
            */
            document.getElementById("demo").innerHTML = "Hello " + name;
            /*
                Output: Hello 
                It does not show any error.
            */
            document.getElementById("demo").innerHTML = "Hello " + name_aaa; 
            /*
                sss.html:21 Uncaught ReferenceError: name_aaa is not defined
                at Object.show (sss.html:21)
                at sss.html:29
            */
        }
    };
    persion.show();
</script>

</body>
</html>

您能帮忙解释为什么name不是错误但name_aaa会抛出错误吗?

0 个答案:

没有答案