S
Stoyan
Hi group,
Since JS is everywhere (client, server, desktop...), it makes sense to
think how to write code that could run in environments other than the
one originally in mind. Perhaps it's not a good idea to use `window`
when the code could possibly run in environment that has no idea what
`window` is.
so this pattern came to mind:
var global = function(){return this;}();
However this apparently won't work in ES5 strict which aims to prevent
errors from calling constructors without `new`
Another hacky idea is to call a function that assumes global when
`this` is unusable. Andrea Giammarchi came up with the shortest form
(http://twitter.com/WebReflection/status/9406207674)
var global = [].sort.call(null);
The question is - is that ES5 strict-safe? Or any other ideas how to
get access to the global without hardcoding its name?
Of course alternatively the new environment could simply define window
and problem is solved. E.g. if in some environment the global object
is called `foo`, then just do a global var window = foo; and call it a
day
Thoughts?
Thanks,
Stoyan
Since JS is everywhere (client, server, desktop...), it makes sense to
think how to write code that could run in environments other than the
one originally in mind. Perhaps it's not a good idea to use `window`
when the code could possibly run in environment that has no idea what
`window` is.
so this pattern came to mind:
var global = function(){return this;}();
However this apparently won't work in ES5 strict which aims to prevent
errors from calling constructors without `new`
Another hacky idea is to call a function that assumes global when
`this` is unusable. Andrea Giammarchi came up with the shortest form
(http://twitter.com/WebReflection/status/9406207674)
var global = [].sort.call(null);
The question is - is that ES5 strict-safe? Or any other ideas how to
get access to the global without hardcoding its name?
Of course alternatively the new environment could simply define window
and problem is solved. E.g. if in some environment the global object
is called `foo`, then just do a global var window = foo; and call it a
day
Thoughts?
Thanks,
Stoyan