E
enaeher
Hello,
I've been using the technique described at
http://onjs.com/blog/index.php?title=faux_javascript_constructors_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
I've been using the technique described at
http://onjs.com/blog/index.php?title=faux_javascript_constructors_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