Why this is not point to js global scope in the code below ?
<html>
<head></head>
<body>
<script type="text/javascript">
var valueHolder = {
value: '',
setValue: function(newValue) {
this.value = newValue;
},
getValue: function() {
return this.value;
}
}
valueHolder.setValue("hello world");
alert(valueHolder.getValue()); // return "hello world"
alert(valueHolder.value); // return "hello world"
alert(window.value); // return "undefined"
</script>
</body>
</html>
Depends on the reference to the function (c.f. 11.2.3 of the spec):
When referred to in context
thisis set to the relevant context (valueHolderin your example). In my example above, the function definitions are clearly identical, but the function references are not in the context of any object, and in that casethisis set to the global context (window).