Hi,
After thinking about a number of ways it could be done, I thought I'd
appreciate your experienced opinion.
Imagine I wished to write a wrapper for (say) DOM objects, in order to
make a few more methods available and possibly add custom properties (as
it isn't very tidy to add properties to the objects themselves, seeing
as they are not native javascript). Furthermore, it should:
- be simple and not verbose to use, as transparent as possible indeed
- while keeping in line with the previous, do not deviate needlessly
from javascript idiomatic practices
- be memory/resource saving
- add as small a speed overhead as possible
Code such as the following should work:
var ref=document.getElementById('Menu');
var enhancedRef=DOMNode(ref);
enhancedRef[...some DOM property...]=...; // should call the implied
accessor and be reflected in the render engine
var x=enhancedRef.absolutePosition().x // example of add-on method
Questions:
- is there any reason why this should not be done?
The simplest solution that comes to mind is to take the reference as the
prototype and add the rest each time:
function DOMNode(aDOMReference)
{
var impl=function(){};
impl.prototype=aDOMReference;
var rest=ImplRepository.implDOMNode;
for(var i=0; i<rest.length; ++i)
impl.prototype[rest[i].name()]=rest[i]; // using some
Function.prototype.name
return impl;
}
function ImplRepository()
{
ImplRepository.implDOMNode =
[
function absolutePosition(){...}
]
}
However, I'm not at all convinced by this approach, as it creates a
whole function each time. I can see that a different one is needed for
each node type, as they all have different 'prototypes', but is there
any way to at least add the rest of the elements to the prototype in a
tidier way?
If there were something similar to Smalltalk's interception of
#doesNotUnderstand, one could just use the same function for all DOM
objects, pass it the DOM reference, and let it use the DOM object to
resolve an identifier if it didn't belong to the function.
Thanks for your attention,
--
am
laurus : rhodophyta : brezoneg : smalltalk : stargate