473,386 Members | 1,674 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,386 software developers and data experts.

Hybrid classes based on DOM objects

Hello,

I've been using the technique described at
http://onjs.com/blog/index.php?title...rs_pt_3_return
to create "constructors" for objects which are DOM elements and also
have additional methods, state, etc., e.g.:

function foo (state) {
var that = new document.createElement('div');
that.state = state ? true : false;
that.toggleState = function () { that.state = !(that.state); }
return (that);
}

document.getElementById('someID').appendChild(new foo (false));

This is useful in cases where I'd otherwise be trying to maintain, in
parallel, a tree of DOM elements and a corresponding tree of objects.
It eliminates a good deal of the bookkeeping involved. But it kind of
breaks some things, in that it's not possible to define foo.prototype
(well, you can, but the properties of the prototype aren't applied to
the object returned by new foo()). Inheritance, in particular, gets to
be a pain. Is there a better way to do what I'm trying to do? Is the
whole idea wrong-headed?

Thanks,

--Eli

Jun 16 '06 #1
3 1188
en*****@gmail.com wrote:

Hi,
function foo (state) {
var that = new document.createElement('div');
that.state = state ? true : false;
that.state=state; // :)

that.toggleState = function () { that.state = !(that.state); }
return (that);
}

document.getElementById('someID').appendChild(new foo (false));
You're using a factory as a constructor, which does work but remains
pretty inelegant. Why not directly use a factory, e.g. makeFoo() ?

But it kind of
breaks some things, in that it's not possible to define foo.prototype
(well, you can, but the properties of the prototype aren't applied to
the object returned by new foo()).


JFYI, in Gecko-based browers, DOM objects are also prototyped and their
prototype can be manipulated; however, since this is not implemented in
other browsers, that technique is not usable for the web.

Now, I a bit perplexed at what pattern you're running after; couldn't
your problem simply be circumvented by using a privileged static object
as "explicit prototype", for instance

var makeExtendedDiv = (function(){
var proto={
'bar' : 42
}

return function(state) {
var div=document.createElement("div");
div.state=state;
div.getPrototype=function(){return proto;};
return div;
}
})();
Jun 16 '06 #2
Elegie wrote:
You're using a factory as a constructor, which does work but remains
pretty inelegant. Why not directly use a factory, e.g. makeFoo() ?
Yes, that would be clearer -- thanks.
Now, I a bit perplexed at what pattern you're running after; couldn't
your problem simply be circumvented by using a privileged static object
as "explicit prototype", for instance


But anything in that var 'proto' would still be evaluated per-object,
not per-class, right? Or am I mis-reading your code? So far this hasn't
caused me any real problems, aside from the inelegance of not having
prototype properties clearly separated from instance properties, but
I'm wondering if this will cause difficulties down the road.

Basically what I'd like to do is subclass DOMElement (or whatever the
DOM element class is called), but this doesn't seem possible directly.

Thanks,

--Eli

Jun 16 '06 #3
en*****@gmail.com wrote:

Hi,
But anything in that var 'proto' would still be evaluated per-object,
not per-class, right? Or am I mis-reading your code?
IMBW, but I think you might actually be mis-reading it; the variable
'proto' has been made private static, shared by all instances, by using
a closure.

There are two functions in the example:
[1] the outer function is defined and executed at once; the
makeExtendedDiv variable therefore receives the return value of this
execution, and not the outer function itself (which, since being
anonymously defined, is not accessible after having been executed).

[2] the inner function is returned by the outer function and assigned to
the makeExtendedDiv variable (in javascript, functions are first-class
objects). However, being defined in the body of the outer function, it
still has access to the variables of this outer function, even if the
outer function has already returned (javascript has static scoping).

<URL:http://en.wikipedia.org/wiki/Lexical_scoping>
<URL:http://www.jibbering.com/faq/faq_notes/closures.html>
<URL:http://www.litotes.demon.co.uk/js_info/private_static.html>

Trying to access and modify any property of the 'proto' object with two
instances should provide you with a good illustration.

Basically what I'd like to do is subclass DOMElement (or whatever the
DOM element class is called), but this doesn't seem possible directly.


Well, DOM elements are provided by the host, which may or not have them
work with prototypes; hosts are not required to, but they can do it. In
Mozilla for instance, they have been made prototyped and therefore you
could "subclass" them directly, working with 'HTMLDivElement.prototype'
in your example (even though talking about "class" for javascript
matters is quite inappropriate). However other vendors' products, such
as Internet Explorer or Opera, do not expose such mechanisms, which
eventually means you'll have to work with custom prototypes.
HTH.
Jun 17 '06 #4

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

Similar topics

145
by: David MacQuigg | last post by:
Playing with Prothon today, I am fascinated by the idea of eliminating classes in Python. I'm trying to figure out what fundamental benefit there is to having classes. Is all this complexity...
5
by: Markus Seibold | last post by:
Dear NG, I am working on a student project about a mobile tourism information system and among others I have to answer the question whether to use: - a relational database - a XML-native database...
0
by: vanGogh | last post by:
I have generated classes based on an Xml schema file (xsd) using the XSD.exe tool. My goal is to: - write an application that can parse XML documents - fill objects (based on the generated...
9
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
3
by: Dave | last post by:
Please - anyone that can help. I am getting confusing results while trying to expose a collection from a web service. I have a webservice in which a web method accepts a collection as a...
9
by: Allan Ebdrup | last post by:
I would like to use reflection to find all classes that inherit from my current class, even if they are in another assembly I want to find them if the current project has a reference to that...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.