我试图了解 javascript 对象中变量的范围。但我得到的行为似乎有点偏离了道路。简单地说,如果我有一个将函数定义为变量的对象,那么函数变量将无法访问定义它的对象的其他变量。下面的代码将使事情变得清晰。
<html>
<head>
<script type="text/javascript">
var someObject = {
someVariable : 5,
getVariable: function() {
return someVariable;
}
};
window.onload = function () {
alert(someObject.getVariable());
};
</script>
</head>
<body>
Hello There
</body>
</html>
上面的代码为函数 getVariable() 中的 someVariable给出了“ReferenceError: someVariable is not defined” 。有人想评论这种行为吗?