Because Javascript assigned x as a value, not a reference to the source object.
For example, you could instead:
function setText(x) { document.getElementById('test').value = x; } getText = function() { return document.getElementById('test').value; }
And the value set with setText() will be reflected by getText() , since getText() will also use the value of the reference object, not a copy of the value.
EDIT
As Brian points out, this will be a copy from a link with a global scope:
var test = document.getElementById('test'); function setText(x) { test.value = x; } getText = function() { return test.value; }
http://jsfiddle.net/nLj2A/
The original variable test stores a reference to the element, not the value associated with the element attribute.
source share