Hiding the global javascript context with my own

I’m reading the MDN web docs on globalThis.

It says:

Note: The globalThis property is configurable and writable so that
code authors can hide it when executing untrusted code and prevent
exposing the global object.

I want to do exactly this, creating a sandbox of sorts so that a function can be written which accesses variables in the sandbox without needing to prefix it with anything and also preventing any variables not in the sandbox object from being accessible – like an improved alternative to the deprecated “with” construct which will fail to find anything not in the global context I define.

I wrote a proof of concept running in an ES6 module:

let myContext = { a:1, b:2 }
let f = ()=> {
    let savedGlobal = globalThis
    globalThis = myContext
    let ret = a+b
    savedGlobal.globalThis = savedGlobal
    return ret
}
f()

As an explanation, I am first saving the current value of globalThis to a local variable, as I expect that otherwise the original value won’t be accessible from my new global context. Then I hide the existing global context by overwriting it with my own, which the documentation seems to suggest is possible. Then I try to assign a local variable to the result of a+b which should be 1+2 = 3, then I restore the global context from the local saved value and return the value I calculated.

If I log the variables, I see that globalThis is the Window object before I assign to it, and it’s myContext after, but I still get a reference error “a is not defined” despite the fact that I would expect that it would find it in the now global context “myContext”. It also successfully ran the console.log despite the fact that I would expect it to no longer be visible after reassigning globalThis.