Minimising duplication using ((){})();

Discussion in 'Javascript' started by Andrew Poulos, Jul 16, 2008.

  1. I'm building some code that using this type of structure to interface to
    SCORM compliant LMS:

    var API = (function() {
    // Private variables
    var started = false;

    // Private methods

    // Public methods
    return {
    // Initialise
    Initialize : function() {
    started = true;

    }
    }
    })();


    Unfortunately there are two common standards that need to be satisfied
    and I was hoping to do it with minimal duplication.

    My problems are these:

    1. Some LMS will be referring to 'API' and some will be expecting the
    name 'API_1484_11'. If I add this line

    var API_1484_11 = API;

    does that mean, say, that I could call API.Initialize() and it would be
    the same as calling API_1484_11.Initialize()?

    2. The public methods are identical in content but need to have slightly
    different names for 'API' and 'API_1484_11'. For example, 'Initialize'
    and 'LMSInitialize'. How can I achieve this without duplicating the
    entire method?

    Andrew Poulos
    Andrew Poulos, Jul 16, 2008
    #1
    1. Advertising

  2. Andrew Poulos <> writes:

    ....
    > 1. Some LMS will be referring to 'API' and some will be expecting the
    > name 'API_1484_11'. If I add this line
    >
    > var API_1484_11 = API;
    >
    > does that mean, say, that I could call API.Initialize() and it would
    > be the same as calling API_1484_11.Initialize()?


    Yes. They not only behave the same, they are the same: The same method
    called on the same object.

    > 2. The public methods are identical in content but need to have
    > slightly different names for 'API' and 'API_1484_11'. For example,
    > 'Initialize' and 'LMSInitialize'. How can I achieve this without
    > duplicating the entire method?


    API.LMSInitialize = API.Initialize;
    var API_1484_11 = API;

    API.Initialize();
    API_1484_11.LMSInitialize(); // exactly the same

    .... as long as you don't have the same method name meaning different
    things in the different APIs.

    /L
    --
    Lasse Reichstein Nielsen
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jul 16, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ll

    duplication?

    ll, Nov 9, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    395
    Rocky Moore
    Nov 9, 2003
  2. msnews.microsoft.com

    Duplication of html generated from htmlwebresponse

    msnews.microsoft.com, Mar 28, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    380
    msnews.microsoft.com
    Apr 5, 2005
  3. seb
    Replies:
    3
    Views:
    264
  4. Jarrick Chagma
    Replies:
    28
    Views:
    1,056
    Martin Gregorie
    May 27, 2009
  5. Marty

    Pop-up window keeps minimising

    Marty, Sep 8, 2005, in forum: ASP General
    Replies:
    2
    Views:
    127
    Marty Tech
    Sep 8, 2005
Loading...

Share This Page