SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Dec 2008
    Location
    Virtual Globe Trotter
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Doubt about module pattern in javascript

    Hi,

    I was wondering whats the practical use of writing a piece of js code as follows:
    Code JavaScript:
    (function(){
      var someval = "";
      function f1(){...}
      function f2(){...}
      function f3(){...}
    })();

    Can setTimeout functions be used in this case?

  2. #2
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it can be used.

    I use this construction sometimes to isolate my code from any other code on the page - very useful when you writing library with common function names like "attach", "each", "more", "find", etc.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  3. #3
    SitePoint Member
    Join Date
    Dec 2008
    Location
    Virtual Globe Trotter
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, this form of code will be completely unobtrusive javascript since i cannot call any function from <script> tags?

  4. #4
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's not really what "unobtrusive" means... Anyway, I don't think you're using it correctly. The module pattern is only useful if you return methods or properties, for example:

    Code javaScript:
    var widget = (function(){
        // Do some stuff up here...
        // Return object:
        return {
            show : function() {...},
            hide : function() {...},
            duplicate : function() {...}
        };
    })();
     
    // Use it:
     
    widget.show();
    widget.hide();

    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •