I think you might want to look at a Facade pattern. The idea being you
create a NEWAPP namespace with a whole new API - this acts as a facade on to
the original, monolithic, code.
*original file:*
APP = {
foo : function() {},
bar: function() {},
baz: function() {}
}
(NB - it's irrelevant whether the above is in a closure or not, asuming that
it provides APP.foo to the global namespace)
*new file:*
var NEWAPP = {};
(function(na) {
// passed the "namespace" in as a
// parameter, so it's super easy to
// change any time you like, without
// touching any code inside this
// closure
// you can have "private" functions
var privateFunction = function() {};
// and add the new "public" api
na.publicFunction = function() {
var fooOK = APP.foo(),
barOK = APP.bar();
return (fooOK && barOK);
};
})(NEWAPP);
*implementation file / web page:*
NEWAPP.publicFunction();
In this example, you would create "wrapper" methods in the NEWAPP namespace
that internally call methods inside APP, but in some nicer fashion.
Best,
Pete
On 26 July 2011 19:03, Xavier MONTILLET <[email protected]> wrote:
> APP is public so his properties are too...
> But if you want to acces something APP's methods use but that aren't
> referenced in APP itself, you just can't.
> If i were you, i would insert my code just before the end of the closure.
> On Jul 26, 2011 7:57 PM, "Wilkins" <[email protected]> wrote:
> > Nah. If I were to use the original version, it's all wrapped up in a
> > closure, so I can't access anything using an outside file.
> >
> >
> > On Jul 26, 1:44 pm, "David Marrs" <[email protected]> wrote:
> >> You should have had access to the APP methods in the unmodified example.
> You couldn't just call APP.stuff.doStuff() in your code? Or am I
> missing something here?
> >>
> >> On 26 Jul 2011 18:30, Wilkins <[email protected]> wrote:
> >>
> >> The project I'm working on has an enormous JS file that I didn't want
> >>
> >> to add to. I wanted to access some of the methods defined, so I
> >>
> >> created an adapter file that would load "after" (minfied + combined)
> >>
> >> the main file.
> >>
> >> Here is how the original would look:
> >>
> >> var APP = APP || {};
> >>
> >> APP = {
> >>
> >> stuff : {
> >>
> >> doStuff : function(){},
> >>
> >> doStuffAgain : function(){}
> >>
> >> },
> >>
> >> moreStuff : {
> >>
> >> doMoreStuff : function(){}
> >>
> >> }
> >>
> >> };
> >>
> >> In order to access it's methods, I returned it as an object like this:
> >>
> >> var APP = APP || {};
> >>
> >> APP = (function() {
> >>
> >> return {
> >>
> >> stuff : {
> >>
> >> doStuff : function() {
> >>
> >> },
> >>
> >> doStuffAgain : function() {
> >>
> >> }
> >>
> >> },
> >>
> >> moreStuff : {
> >>
> >> doMoreStuff : function() {
> >>
> >> }
> >>
> >> }
> >>
> >> // etc
> >>
> >> };
> >>
> >> })();
> >>
> >> It seems to work fine. I have access to everything I need in my
> >>
> >> adapter. However, there were never any tests written, so I'm not 100%
> >>
> >> sure EVERYTHING will work.
> >>
> >> Are there any obvious downsides in doing something like this?
> >>
> >> --
> >>
> >> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
> >>
> >> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
> >>
> >> To unsubscribe from this group, send email to
> >>
> >> [email protected]
> >
> > --
> > To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
> >
> > To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
> >
> > To unsubscribe from this group, send email to
> > [email protected]
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
>
--
Pete Otaqui
[email protected]
+44 7949 945542
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]