Best route for object augmentation?

  • Thread starter António Marques
  • Start date
A

António Marques

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.name()]=rest; // 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,
 
A

António Marques

António Marques said:
The simplest solution that comes to mind is to take the reference as the
prototype and add the rest each time: (...)

And this code was so sloppily written that it doesn't even work, but it
was only intented to be illustrative, anyway.
 
M

Matt Kruse

António Marques said:
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
- is there any reason why this should not be done?

It's counter-intuitive?
It adds no value over just having a getAbsolutePosition() function which
takes an object (btw, see
http://www.javascripttoolbox.com/lib/objectposition/)

Further, your 'enhanced' object can no longer be passed to functions which
expect an element reference. Why create an object out of an object, just so
you can call methods on it instead of calling global functions?

It would be great to be able to extend arbitrary html element objects, but
it's just not possible right now in a cross-browser www context.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top