473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Best route for object augmentation?

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
Jun 8 '06 #1
2 1776
António Marques wrote:
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.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jun 8 '06 #2
António Marques wrote:
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.
--
am

laurus : rhodophyta : brezoneg : smalltalk : stargate
Jun 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Swartz | last post by:
Hi all. I'm working here on a small project of mine. I'm not new to programming, but I'm new to PHP. You have to understand that I'm coming from C++, OOP world, so my code might seems a little...
8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
8
by: Gawain Lavers | last post by:
I have a script which uses a lot of object augmentation (in order to extend the functionality of DOM elements), and I'm clearly stressing out my browser (I rapidly get to a point where Firefox is...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
5
by: reycri | last post by:
Hi, I need to be able to do this: var func = new Function("var me = <selfRef>; alert(me.params);"); func.params = "This is a test parameter"; window.setTimeout(func, 500); Basically, I...
4
by: Daniel | last post by:
Hi, I was reading Douglas Crockford's article on prototypal inheritance at http://javascript.crockford.com/prototypal.html. I think it also relates to a post back in Dec 2005. The mechanism...
14
by: julie.siebel | last post by:
I've been wrestling with a really complex page. All the data is drawn down via SQL, the page is built via VBScript, and then controlled through javascript. It's a page for a travel company that...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.