S
Stevo
I'm defining multiple objects that look basically like this, although
they'll have many more properties in the real case:
var myobj123={a:1,b:2,c:3,requires:"libv4"};
document.write(script tag to include v4 lib js file)
var myobj456={a:44.b:55,c:66,requires:"libv5"};
document.write(script tag to include v5 lib js file)
Just some objects with some properties. Nothing special going on there.
You see each of them has a requires field that specifies which version
of a library they need.
OK now we have the objects defined and the lib js files will define the
lib functions like this:
var libv4={};
libv4.func1=function(){};
libv4.func2=function(){};
libv4.func3=function(){};
var libv5={};
libv5.func1=function(){};
libv5.func2=function(){};
libv5.func3=function(){};
The two libs defined the same functions with the same names (in Java
terms you might say they implement the same interface). Now I want to
make the myobj123 and myobj456 retroactively inherit those functions. I
could do it like this:
myobj123.libfuncs=window[myobj123.requires];
myobj456.libfuncs=window[myobj456.requires];
and then I can call myobj123.libfuncs.func1(); which would use the v4
func, but myobj345.libfuncs.func1() would use the v5 func.
From a functionality point of view, this achieves what I need. I could
just go and build it like this and it'd work fine.
I'm pretty sure it's possible to achieve the same thing though without
having to have the libfuncs var pointing to one of the libs so that I
can just call myobj123.func1()
I tried this without success:
myobj123.prototype=window[myobj123.requires];
the func1 function isn't found with myobj123.func1() but in IE8's
developer tools I see that myobj123 has a prototype property that has a
func1 function. I suspect that I could call myobj123.prototype.func1()
but that's just the same as the libfuncs var really.
At this stage of development, I can rewrite to suit a better design as I
wanted to get the structure worked out before I write any real code.
The only limitations I have (caused by the existing project that I'm
building this into) is that the data objects have to exist first, and
the library code is included afterwards and then be added to the
objects. So creating the libraries and then creating the object
afterwards can't be done.
they'll have many more properties in the real case:
var myobj123={a:1,b:2,c:3,requires:"libv4"};
document.write(script tag to include v4 lib js file)
var myobj456={a:44.b:55,c:66,requires:"libv5"};
document.write(script tag to include v5 lib js file)
Just some objects with some properties. Nothing special going on there.
You see each of them has a requires field that specifies which version
of a library they need.
OK now we have the objects defined and the lib js files will define the
lib functions like this:
var libv4={};
libv4.func1=function(){};
libv4.func2=function(){};
libv4.func3=function(){};
var libv5={};
libv5.func1=function(){};
libv5.func2=function(){};
libv5.func3=function(){};
The two libs defined the same functions with the same names (in Java
terms you might say they implement the same interface). Now I want to
make the myobj123 and myobj456 retroactively inherit those functions. I
could do it like this:
myobj123.libfuncs=window[myobj123.requires];
myobj456.libfuncs=window[myobj456.requires];
and then I can call myobj123.libfuncs.func1(); which would use the v4
func, but myobj345.libfuncs.func1() would use the v5 func.
From a functionality point of view, this achieves what I need. I could
just go and build it like this and it'd work fine.
I'm pretty sure it's possible to achieve the same thing though without
having to have the libfuncs var pointing to one of the libs so that I
can just call myobj123.func1()
I tried this without success:
myobj123.prototype=window[myobj123.requires];
the func1 function isn't found with myobj123.func1() but in IE8's
developer tools I see that myobj123 has a prototype property that has a
func1 function. I suspect that I could call myobj123.prototype.func1()
but that's just the same as the libfuncs var really.
At this stage of development, I can rewrite to suit a better design as I
wanted to get the structure worked out before I write any real code.
The only limitations I have (caused by the existing project that I'm
building this into) is that the data objects have to exist first, and
the library code is included afterwards and then be added to the
objects. So creating the libraries and then creating the object
afterwards can't be done.