2

The markup of the current Google Doodle looks like this:

<img alt="84. Geburtstag von Shakuntala Devi" border="0" height="162" src="/logos/doodles/2013/shakuntala-devis-84th-birthday-5315657683959808-hp.gif" title="84. Geburtstag von Shakuntala Devi" width="700" id="hplogo" onload="window.lol&amp;&amp;lol()" style="padding-top:45px">

with the interesting part being onload="window.lol&&lol()"

Just out of curiosity: What does window.functionname&&functionname() actually do? Why not just call the function? Is there any benefit or is it just done because it can be done, to show off? ;)

Kara
  • 6,115
  • 16
  • 50
  • 57
Constantin Groß
  • 10,719
  • 4
  • 24
  • 50
  • 2
    `window.functionname exits, then execute functionname()` – rab Nov 04 '13 at 08:45
  • Wouldn't this throw an error in older browsers, which would have to be caught by checking for typeof(window.functionname) != 'undefined' instead? – Constantin Groß Nov 04 '13 at 08:47
  • 1
    @Connum: No, object properties that dont exist have always been undefined without throwing errors and undefined have always evaluated to false on all browsers since Netscape's original implementation of javascript. The scenario you're thinking is `variable_name&&variable_name()` which will throw an error on all browsers, new or old. – slebetman Nov 04 '13 at 08:54

1 Answers1

4

&& will return the left hand side if it is false, otherwise it will return the right hand side.

If window.functionname is undefined, then it will be false.

Only if it is defined will the right hand side be evaluated and the function called.

If you try to call undefined as a function, then an exception will be thrown and the script will halt. This test is to prevent that from occurring.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335