Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #17918

Source code "objectifying" technique?

From JJ <jaejunks@nah.meh>
Newsgroups comp.lang.javascript
Subject Source code "objectifying" technique?
Date 2013-01-03 10:34 +0000
Organization A noiseless patient Spider
Message-ID <XnsA13DB38A51C6jj@0.0.0.55> (permalink)

Show all headers | View raw


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);

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?

Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll thread


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