ungap / global-this

A cross engine globalThis

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

new shorter version?

jimmywarting opened this issue · comments

came up with this solution:

(f=>f().globalThis=f())({f(){return this}}.f)

haven't tested it much, I think it's safe to use...

  • shorter
  • don't modify any prototype
  • safe from CSP eval stuff

you know how if you do

var { getUserAgent } = navigator
getUserAgent()

you will get Illegal invocation error since getUserAgent loses it's context cuz you destructed it from the navigator.?

well, if you create an object and do the same thing who's function only return this

var foo = {
  fn () {
    return this
  }  
}

var fn = foo.fn
console.log(foo.fn()) // foo
console.log(fn()) // globalThis

then you might get away with my minified version of just doing this:

(f=>f().globalThis=f())({f(){return this}}.f)

nvm, don't work in strict mode

the prototype is modified and cleaned up right away, and since the current logic was suggested by a google employee with very high attention to details and performance, I think there's no reason to change the script as is.

It's also already safe for CSP, and its size irrelevant compared to the rest of the JS shipped with average websites so, thanks for trying, but I think this module is "done" as it is ;-)