function redefinition and the standard

L

lallous

Hello,

In IE, when you redefine a function it will be overwriten by the latest
declaration.

Is that by the standard or by this browser implementation?
 
G

Grant Wagner

lallous said:
Hello,

In IE, when you redefine a function it will be overwriten by the latest
declaration.

Is that by the standard or by this browser implementation?

function abc() { alert('hi'); }
function abc() { alert('bye'); }
abc();

is equivilent to

var abc = new Function("alert('hi');");
var abc = new Function("alert('bye');");
abc();

so unless a browser is doing something interesting parsing the included
JavaScript, it will always execute the last function defined with a
particular name.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
L

Lasse Reichstein Nielsen

Grant Wagner said:
function abc() { alert('hi'); }
function abc() { alert('bye'); }
abc();

is equivilent to

var abc = new Function("alert('hi');");
var abc = new Function("alert('bye');");
abc();

While the conclusion is correct, this statement is slightly incorrect.
(I.e., ignore this unless you are a pedant like me :)

When Javascript interprets a block of code, it first treats all the
function declarations in the order they appear, i.e., it creates the
local variable and assigns their value. Then it treats all the
variable declarations and declares the variables (i.e., creates them
as properties of the variables object if they don't exist already),
but does not assign a new value, even if the declaration looks like
"var foo=42;". That is treated like "var foo;foo = 42;".
Then it executes the code, including the assignments to variables.

So

var foo = 42;
function foo(){}
alert(foo);

will alert "42".

In any case, declaring a function or variable will create it as a
local variable, a property of the variable object of the
scope. Creating it twice is allowed, and the last assignment to it
will win (functions first, in the order they appear, then assignments
in the normal code, in the order the are executed).


/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top