Friday, August 26, 2005

Javascript function overriding, Part 2

Continue from the part 1, I will give a more detail explaination to what I say in Part 1.

Using the same example :
Method 1
<script language="javascript">
var base_foo;

function foo()
{
alert('foo is called');
}

base_foo = foo;

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

The first time you create function foo(), your code will be loaded into a arbitary memory location, say for example 0x001a.

Your function pointer foo will be set to point to address 0x001a.

Next you assign base_foo to point to the same memory address pointed to by foo

Now, your override function foo() to do different thing. The script engine will load your new code into the same memory location (0x001a).

And both your function pointer foo and base_foo will remain point to the same address.


Moving to method 2:
Method 2
<script language="javascript">
var base_foo;

function foo(yourname)
{
alert('Hello ' + yourname);
}

base_foo = foo;

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


The first time you create function foo(), your code will be loaded into a arbitary memory location, say for example 0x001a.

Your function pointer foo will be set to point to address 0x001a.

Next you assign base_foo to point to the same memory address pointed to by foo.

Now you override function foo() to do different thing:

foo = function(yourname)
{
// your new code
}

What happen now is your new code is loaded into a new memory location, say (0x002a).

Your function pointer foo will now be changed to point to address 0x002a. But base_foo remain point to 0x001a.

In the next part, I will give an example of how to prove this.

Labels:

2 Comments:

At 5:28 PM, Blogger wow power leveling said...

If I were gold für wow a boy again,world of warcraft gold I would practice perseverance wow gold cheap more often,maple meso and never give up a thing because it was or inconvenient. If we want light,Maple Story Account we must conquer darkness. Perseverance can sometimes equal genius in its results.wow gold kaufen “There are only two creatures,”cheap maplestory mesos syas a proverb, “who can surmount the pyramids—the eagle and the snail.” If I were a boy again,wow geld I would school myself into a habit of attention;maple mesos I would let nothing come between me and the subject in hand.maple story power leveling I would remember that a good skater never tries to skate in two directions at once.billig wow gold The habit of attention becomes part of our life, if we begain early enough. I often hear grown up people say maple story items“ I could not fix my attention on the sermon or book, although I wished to do so” , wow powerlevelingand the reason is, the habit was not formed in youth. If I were to live my life over again,wow leveling I would pay more attention to the cultivation of the memory. I would strengthen that faculty by every possible means,wow power leveling and on every possible occasion.maplestory powerleveling It takes a little hard work at first

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

Hi your explanation was very helpful to me. Thnx. Keep writing....

 

Post a Comment

<< Home