I'm just wondering whats the correct way to check if a function is undefined with a script which will be closured in advanced mode and how to access global functions?
E.g. an example of checking if google analytics is loaded:
typeof window["ga"] == "function"
typeof window["ga"] !== "undefined"
But is the following bullet proof with closure compilers as well?
typeof window["ga"] == function
typeof window["ga"] !== undefined
And what about localStorage. Like magic the following works in chrome:
if (localStorage != undefined ) myvariabel = localStorage.getItem('myvariable')
But in seems to me like dirty coding. In my mind the following would be correct or?
if(typeof localStorage != "undefined")
// or even better the following since per definition local storage is an attribute of window:
if(typeof window["localStorage"] != "undefined") myvariabel = window["localStorage"].getItem('myvariable')
In this context is .getItem safe to use in advanced compiler mode or do I have to code:
if(typeof window["localStorage"] != "undefined") myvariabel = window["localStorage"]["getItem"]('myvariable')