Wednesday, August 24, 2005

Javascript function overriding, Part 1

Disclaimer : All the javascript code, explaination and description in this blog series is particular to Internet Explorer only.

In Javascript, you can overide a function using the following method :
Method 1
<script language="javascript">
function foo()
{
alert('foo is called');
}

function foo()
{
alert('override foo is called');
}
</script>

If you call foo(), it will display 'override foo is called'.

Recently, I discover another way of overriding function :
Method 2
<script language="javascript">
function foo(yourname)
{
alert('Hello ' + yourname);
}

foo = function(yourname)
{
alert('(overide) Hello ' + yourname);
}
</script>

Both method will accomplish the same thing. However, they are semantically different.

Using method 1, what happen is the new code will over-write the existing code at the same memory address.

In method 2, the new code will be loaded into a different memory address and the function pointer foo will be changed to point to this new memory address.

What does this mean ? I will give an example and show you how to prove what I say in my next post.

Labels:

3 Comments:

At 7:30 PM, Blogger Sven De Bont said...

Nice article... love the examples

 
At 6:20 PM, Anonymous Anonymous said...

Link to part 2 is wrong. It has an additional 'r' within 'override':)

 
At 4:39 PM, Blogger Unknown said...

Thanks, this helped me out!

 

Post a Comment

<< Home