...

View Full Version : Javascript Inheritance



SavedLinuXgeeK
08-27-2006, 03:50 PM
Hi,

I am dealing with a very unique problem, one that I have eventually come to assume requires the use of eval. I know that eval is highly inefficient, and so I hope to keep it down to about 3 or 4 evals called at the beginning of a page load, and thats it. Essentially I will need to call eval on every inheritance.
The problem stems from the fact that in order to truly acheive 'classical-inheritance', I need to the ability to call parent functions, and the parent's parent functions. Right now I have a slightly dirty method of calling the same function down an entire chain, through all ancestors. But if I call a parent's function, and from within that parent function, it calls another function, I want that function to be called from the scope of the Parent, not the Child. And right now, it calls the function from the scope of the Child.
My idea is to use tokens, like $this, and $parent within the the function definitions, and then replace those tokens with actual references to the Current class and Parent class, ie. Parent.prototype[method].apply(this, arguments); replaces $parent.method(arguments). The only way I can seem to fix this issue is by doing an eval, as I found no acutal way of applying a variable number of arguments to the Function object.
If anyone else has a better idea I am more than happy to listen. Thank you.

vwphillips
08-27-2006, 05:15 PM
eval('FunctionName'+'()');

=

window['FunctionName']();

but better

Beagle
08-28-2006, 04:39 PM
I'm not sure what you're problem here is, if you have an object prototype that defines methods and members, and then you have another object that uses that prototype and adds to it you still have access to the "parent"'s methods and members unless you redfine them. The prototype chain keeps going back as long you want it to.

Perhaps if you could provide some code to explain what you're trying to do, we could help you avoid eval altogether.

iota
08-28-2006, 05:21 PM
There are now JavaScript Prototype Class Framework out there.

You can easily use them to create true OO Javascript.

A1ien51
08-28-2006, 06:07 PM
I think you may want to look at Appenix B of my book. Sun has it avaialble on their site here:

http://java.sun.com/javascript/ajaxinaction/Ajax_in_Action_ApB.html

Hope that helps.

Eric



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum