If We have
var randomname = {};
randomname.attribute = 'something';
function randomname(){
alert(randomname.attribute);
}
randomname();
Will javascript throw any errors?
Update
So, We know that we cannot have a object have the same name as a function.
Why is this?
Should javascript not be able to tell what you are after by the way you call it?
It should give you a
TypeErrorexception -for trying to invoke an object-, the behavior observed in the Firebug’s console is not right…FunctionDeclaration‘s are hoisted to the top of their enclosing scope, your code is actually executing in this order:When entering to an execution context, the Variable Instantiation process (aka Declaration Binding Instantiation in ES5) defines the properties on the Activation Object (AO, this is a non-reachable object that holds identifiers of variables, functions declarations and function arguments in the local scope) in the following order:
FormalParameterListidentifiers (for Function Code)FunctionDeclarationidentifiersVariableDeclarationidentifiersA
VariableDeclarationwill not overwrite an identifier has been already defined in the AO (e.g. by one of the first two steps), but the assignment will do it at run time.For example:
But if an assignment is made, the value of the identifier will be replaced, for example:
In the above example, when the function is invoked, -but before the code execution- the
fooidentifier is set up on the Activation Object (aka Variable Object).First it gets assigned the value of the formal parameter, the value
'string', then allFunctionDeclaration‘s on the Function Body are examined, a function namedfoois found, then thefooidentifier will point to that function.After that, all Variable Declarations are examined, we have one with the identifier
foo, but its value is respected in this time -remember that the function hasn’t been executed-.At this point, the function is ready to be executed, its lexical and variable environment is setup.
The first thing that will be executed in the function, is the assignment
foo = {};, which replaces the reference to the function that we had before.Why it behaves differently on other browsers and Firefox?
Because:
Since Firebug evaluates code by wrapping inside a
withstatement, this causes theFunctionDeclarationto be evaluated in statement context -a Mozilla’s Function Statement-.To show the behavior of the Function statement on Mozilla implementations, consider the following example:
In Mozilla you will get
'true!', while in other implementations you will get'false??!'– even on IE-.That’s because the function definition was made at run-time, in Statement context (in side the true branch of the
if), while in other implementations, the function declarations are evaluated at parse time.The above example should actually produce a
SyntaxErrorexception on any implementation, but that doesn’t happen on any of them…A
FunctionDeclarationshould be allowed only, in global code or directly in the FunctionBody of a function.A lot of people use interchangeably the terms Function Declaration and Function Statement but that’s totally wrong, ECMAScript doesn’t define a Function Statement, is a non-standard feature.
The specification has a brief note on this issue:
Try to run your code in the global execution context -in a simple
<script>element- and you will see it crash also on Firefox:You can find the above example here, make sure to open the Firebug’s console and you’ll see the same error you get on other browsers.