B
Benjy Borda
I assume this question must have been answered in the past, but I have
been searching the archives the last couple of days and cannot find a
solid answer to my problem.
I have a couple of properties that need to be created with every new
object created. To simplify, assume the only property I need to do
this with is called 'dom', which stores a dom object. In this context,
every object will need to create its own dom to avoid sharing of the
same dom object. A solution I have is like so:
var clone = (function(){
function F(){}
return (function(o){
F.prototype = o;
return new F();
});
})();
var proto_root = { /* bunch of methods */};
function Root() {
this.dom = document.createElement('div');
}
Root.prototype = proto_root;
var proto_widgetA = clone(proto_root);
var proto_widgetA.someNewProp = function() { //does something}
function WidgetA() {
Root.call(this); // to set the dom property
// set some specific "widgetA" properties
}
WidgetA.prototype = proto_widgetA;
Then create new widgetA like: var w = new WidgetA();
There will eventually be lots of widgetXYZ constructors and more
objects that inherit from those
This will WORK, but I can't help but feel like it is not the correct
way to accomplish such a thing in a prototype based language.
Inheriting properties like this makes me feel like I am coupling these
constructors too much together and gives me a feel of creating classes
in a class-based language.
An alternative I could come up with that "feels" more prototypical:
function addDom(o) {
o.dom = document.createElement('div');
return o;
}
var root = { /* bunch of methods */};
var proto_widgetA = clone(root);
var proto_widgetA.someNewProp = function() { //does something}
function widgetA() {
var w = addDom(clone(proto_widgetA)); // note the addDom call
here
// add widgetA stuff
return w;
}
and create widgetA's like: var w = widgetA();
The problem I see with this is that there may eventually end up with
things like:
addDom(addSomething(addSomethingElse(clone(obj)));
Which seems ugly to me
What are your thoughts on my two proposed methods? How would you solve
the same problem?
been searching the archives the last couple of days and cannot find a
solid answer to my problem.
I have a couple of properties that need to be created with every new
object created. To simplify, assume the only property I need to do
this with is called 'dom', which stores a dom object. In this context,
every object will need to create its own dom to avoid sharing of the
same dom object. A solution I have is like so:
var clone = (function(){
function F(){}
return (function(o){
F.prototype = o;
return new F();
});
})();
var proto_root = { /* bunch of methods */};
function Root() {
this.dom = document.createElement('div');
}
Root.prototype = proto_root;
var proto_widgetA = clone(proto_root);
var proto_widgetA.someNewProp = function() { //does something}
function WidgetA() {
Root.call(this); // to set the dom property
// set some specific "widgetA" properties
}
WidgetA.prototype = proto_widgetA;
Then create new widgetA like: var w = new WidgetA();
There will eventually be lots of widgetXYZ constructors and more
objects that inherit from those
This will WORK, but I can't help but feel like it is not the correct
way to accomplish such a thing in a prototype based language.
Inheriting properties like this makes me feel like I am coupling these
constructors too much together and gives me a feel of creating classes
in a class-based language.
An alternative I could come up with that "feels" more prototypical:
function addDom(o) {
o.dom = document.createElement('div');
return o;
}
var root = { /* bunch of methods */};
var proto_widgetA = clone(root);
var proto_widgetA.someNewProp = function() { //does something}
function widgetA() {
var w = addDom(clone(proto_widgetA)); // note the addDom call
here
// add widgetA stuff
return w;
}
and create widgetA's like: var w = widgetA();
The problem I see with this is that there may eventually end up with
things like:
addDom(addSomething(addSomethingElse(clone(obj)));
Which seems ugly to me
What are your thoughts on my two proposed methods? How would you solve
the same problem?