-1

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')
peterh
  • 11,875
  • 18
  • 85
  • 108
Manuel
  • 9,112
  • 13
  • 70
  • 110

1 Answers1

2

The typeof operator will give you a string describing the type.

function is not a string, and expects to be followed by certain characters. typeof window["ga"] == function will therefore throw an exception.

typeof will always give you a string. A string is never an undefined value. typeof window["ga"] !== undefined will always give you a true result.

So no. Not using strings isn't bullet proof. Quite the opposite; it won't work anywhere.


And what about localStorage

This is no different to any other variable.

Like magic the following works in chrome

if (localStorage != undefined )

Now you are testing if the variable is equal to undefined, not if using typeof is. That's a completely different problem.

That will work… but only if:

  • undefined hasn't been masked by a different variable that is defined and
  • localStorage has been declared

If it hasn't been declared that you will get a ReferenceError and your script will abort (unless you are using try/catch).

So no. Never compare a variable directly to undefined.

There is a slim possibility that undefined isn't what you think it is. Most of the time you care about the variable being declared as well as being defined. And the rest of the time you can continue to use typeof because it works reliably everywhere and using it as a matter of habit is better then mixing and matching and sometimes getting it wrong.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I think won't work anywhere isn't true either. Just checked the undefined and it seems that at least chrome is casting it as type allowing dirty coding. What if localStorage is used. If localStorage is not available its not an object but type of undefined. Thus comparing types instead of typeof as string is working if not closured. At least in chrome, but is it safe? – Manuel Jan 22 '15 at 10:05
  • thx Quentin. I further tried to specify the question because looking at it in more detail made me unsure about the usage of localStorage.getItem in a closure context. – Manuel Jan 22 '15 at 10:15