One option is to take advantage of the fact the global object can have properties defined on it that are implicitly in-scope. In a web-browser the Window object is the global object, so this:
<script>
var foo = 123;
function bar() { console.log( foo ) };
bar();
</script>
Is the same as this:
<script>
document.window.foo = 123;
function bar() { console.log( foo ) };
bar();
</script>
Is (more-or-less) the same as this:
<script>
Object.defineProperty( window, "foo", { value: 123 } );
function bar() { console.log( foo ) };
bar();
</script>
So we can abuse Object.defineProperty to get the effect you want, with the caveat that it won't work inside JavaScript scopes where global's properties are not accessible.
<script>
function createMagicVariable( name, func ) {
var propDef = {
get: func
};
Object.defineProperty( window, name, propDef );
}
</script>
Used like so:
<script>
function getRandom() { return Math.random(); }
createMagicVariable( 'foo', getRandom );
console.log( foo );
console.log( foo );
console.log( foo );
</script>
let myThing = { get myVar() { /*Check sports scores here */}