javascript - What's a safe way to define a function if it's not defined already -


I have some code that tries to define foobar if it does not Is already defined:

  if (! Fobar) {function fubar () (warning ("Hello from hello"); }}  

Calling foobar works in Chrome outside that block, but not in Firefox (see - Firefox I think, walking around that announcement - this apparently).

I think this is a better way to write:

  function foobar_impl () {alert ("hello from foobar"); } If ('foobar' == type window.noFunc) {var foobar = foobar_impl; }  

But I accept any other ideas or can read some documents.

It looks quite idiotic:

  var foobar = foobar | | Ceremony () {/*...*/};  

Comments