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

  • SEARCH
  • Home
  • 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 1101537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:03:01+00:00 2026-05-17T01:03:01+00:00

I’m trying to define a way in which I can control the access to

  • 0

I’m trying to define a way in which I can control the access to methods for the javascript we use at work (Part of a coding standards improvement drive).

The pattern I was planning to use was fine until I thought about what we do with 3rd party script methods.

How can I tweak my code to allow access to the private function from inside the jQuery method?

var NameSpace = new function () {

    // My private function I want to access.
    var privateFunction = function () {

    };

    this.publicFunction = function () {

        // I can access my private function here.
        privateFunction();

        jQuery(window).resize(function () {

            // But not here :(
            privateFunction();

        });
    };
};
  • 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-05-17T01:03:01+00:00Added an answer on May 17, 2026 at 1:03 am
        // I can access my private function here.
        privateFunction();
        jQuery(window).resize(function () {
            // But not here :(
            privateFunction();
        });
    

    Yes you can. JavaScript is statically scoped. You can access all the variables and functions of each enclosing scope in that resize callback, including privateFunction (as long as you haven’t shadowed it by defining a privateFunction in a nested scope).

    What you can’t do is access any of those local variables outside the enclosing function.

    I’m trying to define a way in which I can control the access to methods for the javascript we use at work (Part of a coding standards improvement drive).

    I would suggest that this is a pointless exercise.

    Namespacing is good for avoiding unwanted name clashes. What truly ‘private’ members are for is strictly enforcing rigid security boundaries. But this is JavaScript: you aren’t running code with different access levels and sandboxing here, like you might in Java. You don’t have to blindly reproduce the security model of Java in JavaScript. Who is the ‘attacker’ here? Yourself? Other coders in your team?

    Data hiding and encapsulation is good practice, but you don’t need to strictly enforce privateness to achieve this. Indeed, having real privates may make debugging and prototyping tasks more difficult. Consider the Python-like approach where you simply mark members that aren’t supposed to be used from outside, for example with a leading underscore. Anyone using a pseudo-private member knows they’re doing something they shouldn’t, and hopefully have a good temporary reason for doing so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.