Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Periods in Javascript function definition (function window.onload(){}) [duplicate]

Tags:

javascript

Possible Duplicate:
JavaScript Function Syntax Explanation: function object.myFunction(){..}

I've seen some (legacy) javascript code recently that looks like:

function window.onload(){
  // some code
}

This doesn't look like valid javascript to me since you can't have a period in an identifier, but it seems to work in IE8. I assume it's the equivalent of:

window.onload = function(){}

I've tried the same code in Chrome and IE9 and both of them raise syntax exceptions, so am I correct in thinking that this "feature" of IE8 is some non-standard function definition that should be replaced? The code in question is only sent to IE browsers, so that's probably why I haven't run into this issue before.

like image 583
nemec Avatar asked Oct 08 '12 15:10

nemec


1 Answers

For Javascript on recent browsers, you can usually refer to ECMAScript and clearly, this isn't allowed in ECMAScript :

ECMAScript spec on function definition :

The production FunctionDeclaration : function Identifier ( FormalParameterListopt ){ FunctionBody } [...]

Create a property of the current variable object (as specified in 10.1.3) with name Identifier

SO on valid names (just in case somebody would think "window.onload" is the name of the function, which thus would be window.window.onload)

like image 186
Denys Séguret Avatar answered Oct 17 '22 08:10

Denys Séguret