I was trying to understand the scope of variables in javascript objects. But the behavior that I am getting seems to be a little off the road. To put it simply, if I have an object that defines a function as a variable, then the function variable is not able to access other variables of the object in which it is defines. The code below will make things clear.
<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>
The above code gives "ReferenceError: someVariable is not defined" for the someVariable in function getVariable(). Would anyone like to comment on this behavior?
someObjectis a variable that points to an object. ButsomeVariableis the name of a property on that object. Variables and object properties are very different things.[[Prototype]]chain, stopping atnull. The second is resolved as local parameters of an execution context and proceeds along the scope chain, which is a sequence of execution contexts (Lexical Environments) stopping at the global object.