Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17919
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Source code "objectifying" technique? |
| Date | 2013-01-03 19:32 +0100 |
| Organization | albasani.net |
| Message-ID | <kc4ir7$bh7$1@news.albasani.net> (permalink) |
| References | <XnsA13DB38A51C6jj@0.0.0.55> |
On 2013-01-03 11:34, JJ wrote:
> Sometimes I need some a library from the web, but its functions are all
> globally defined instead of object based. For example:
>
> var config=123;
> function abc(){
> //does something
> }
> function def(){
> //does something else
> }
> function ghi(a){
> a=a||config;
> abc(def(a));
> }
> function jkl(a){
> a=a||config;
> def(a);
> }
>
> I want to put all those functions in a self contained object so that
> accessing them would be like:
>
> theLib.abc();
> theLib.ghi();
> theLib.abc();
> theLib.abc(456);
Okay, as long as you're aware of that calling abc() is _not_ the same
thing as calling theLib.abc(). At least make sure to check if any of the
functions use |this|.
> Usually I would convert each functions including all of the references
> like below.
>
> theLib={};
> theLib.config=123;
> theLib.abc=function(){
> return theLib.config;
> }
> theLib.def=function(){
> return theLib.config*10;
> }
> theLib.ghi=function(a){
> a=a||theLib.config;
> theLib.abc(theLib.def(a));
> }
> theLib.jkl=function(a){
> a=a||theLib.config;
> theLib.def(a);
> }
>
> As you can see, this is a tedious work. Even for a 2KB code. Is there a
> simpler way to do this?
Depends on the library, but something like this might work:
var theLib = (function () {
// -- start original lib content --
var config = {...};
function abc () {...}
function def () {...} // private helper
function ghi () {...}
// -- end original lib content --
// this will be the public API of the library
return {
abc: abc,
ghi: ghi,
...
};
})();
theLib.abc(123);
Not every collection of functions can be contained in this way, but it
should work for your typical bunch-of-global-functions-because-
-the-author-doesn't-know-any-better library.
- stefan
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Source code "objectifying" technique? JJ <jaejunks@nah.meh> - 2013-01-03 10:34 +0000
Re: Source code "objectifying" technique? Stefan Weiss <krewecherl@gmail.com> - 2013-01-03 19:32 +0100
Re: Source code "objectifying" technique? JJ <jaejunks@nah.meh> - 2013-01-03 20:13 +0000
Re: Source code "objectifying" technique? Stefan Weiss <krewecherl@gmail.com> - 2013-01-03 23:57 +0100
Re: Source code "objectifying" technique? Stefan Weiss <krewecherl@gmail.com> - 2013-01-04 00:44 +0100
Re: Source code "objectifying" technique? JJ <jaejunks@nah.meh> - 2013-01-04 02:00 +0000
Re: Source code "objectifying" technique? Scott Sauyet <scott.sauyet@gmail.com> - 2013-01-03 10:37 -0800
Re: Source code "objectifying" technique? Scott Sauyet <scott.sauyet@gmail.com> - 2013-01-03 10:43 -0800
Re: Source code "objectifying" technique? JJ <jaejunks@nah.meh> - 2013-01-03 20:33 +0000
Re: Source code "objectifying" technique? Scott Sauyet <scott.sauyet@gmail.com> - 2013-01-03 13:09 -0800
Re: Source code "objectifying" technique? JJ <jaejunks@nah.meh> - 2013-01-04 02:33 +0000
csiph-web