当我尝试在js中创建和访问对象的属性时,它让我混淆了未定义的变量:name
和name_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
会抛出错误吗?