Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7785347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:14:27+00:00 2026-06-01T20:14:27+00:00

Conside the following JavaScript code. The function definitions all seem to achieve the same

  • 0

Conside the following JavaScript code. The function definitions all seem to achieve the same thing. Is there any recommended convention for defining the functions which are then ‘revealed’ in the return dictionary object?

var testModule = (function(){
      var counter = 0;

      var localFunc1 = function() {
        return "local 1";
      }

      function localFunc2() {
        return "local 2";
      }

      this.localFunc3 = function() {
        return "local 3";
      }

      localFunc4 = function() {
        return "local 4";
      }

      return {      
        proxy1: localFunc1,
        proxy2: localFunc2,
        proxy3: localFunc3,
        proxy4: localFunc4
      };
    })();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:14:28+00:00Added an answer on June 1, 2026 at 8:14 pm

    I don’t think that there is any real preferred method. The most common setup I’ve seen involves creating all of your methods as local (using var) then returning an object that exposes the public methods.

    A couple of things to note:

    • this.localFunc3 will only work if your object is instantiated with the ‘new’ keyword
    • when returning your object, remove the () from each function so that you are returning a reference to the function and not the function’s returned value
    • localFunc4 will be global since it has no ‘var’ keyword in front of it

    When working with a module pattern like this, I tend to go for something along the lines of:

    var Obj = ( function () {
        var private_static = 'this value is static';
        return function () {
            //-- create return object
            var _self = {};
            //-- create private variables
            var private_variable = 'this value is private';
            var func1 = function () {
                return 'value 1';
            };
            //-- attach public methods
            _self.func1 = func1;
            //-- return the object
            return _self;
        };
    } )();
    

    Some notes about this method:

    • allows for private static variables (if you don’t need private static variables, you can remove the closure)
    • by creating the _self reference first, you can pass a self reference to any objects instantiated from within that need a reference to their parent
    • I like that I can reference all internal functions without _self.whatever or this.whatever, ignoring whether or not they are private or public
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following Javascript code: var a = []; var f = function() {
Consider following JavaScript code (tested in Firefox): function f(a) { if (a == undefined)
Consider the following JavaScript code which has a recursive boom function function foo() {
Consider the following top-level javascript code: if (this.window === window) alert('same'); else alert('different'); //
Consider the following html snippet <!DOCTYPE html> <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-1.4.2.js ></script> <script
Consider the following piece of code: function processParagraph(paragraph) { if (paragraph.charAt(0) === '%') {
Sometimes JavaScript doesn't make sense to me, consider the following code that generates a
please consider the following javascript code: myObject.myMethod();.replace(/\.\w+\(/g, xxx); it gives myObjectxxx); as .myMethod( is
Hai, I am trying to understand few concepts in JavaScript. Consider the following code:
Consider the following code: $('div').click(function(){ $(this).animate({height:100}, 500) $(this).css({opacity:1}); }); Versus: $('div').click(function(){ $(this).animate({height:100}, 500); })

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.