Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > function redefinition and the standard

Reply
Thread Tools

function redefinition and the standard

 
 
lallous
Guest
Posts: n/a
 
      01-26-2004
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?

--
Elias


 
Reply With Quote
 
 
 
 
Grant Wagner
Guest
Posts: n/a
 
      01-27-2004
lallous wrote:

> 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?
>
> --
> Elias


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 <(E-Mail Removed)>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_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-deve...upgrade_2.html


 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      01-27-2004
Grant Wagner <(E-Mail Removed)> writes:

> 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
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: avoid the redefinition of a function Jabba Laci Python 3 09-13-2012 08:23 AM
Re: avoid the redefinition of a function Tim Chase Python 0 09-12-2012 05:34 PM
Re: avoid the redefinition of a function Michael Torrie Python 0 09-12-2012 01:52 PM
Re: avoid the redefinition of a function D'Arcy Cain Python 0 09-12-2012 01:51 PM
avoid the redefinition of a function Jabba Laci Python 2 09-12-2012 01:15 PM



Advertisments