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 6947519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:42:12+00:00 2026-05-27T13:42:12+00:00

I want to implement setter and getter on local javascript variable. Here is an

  • 0

I want to implement setter and getter on local
javascript variable. Here is an example function:

function someThing() {
   var someLocalvariable = '';
}

// with this function I want to 
// return value of someLocalvariable
// also if it is possible to implement
// setter in this way. 
someThing.prototype.getLocalVar = function() {

}

I want variable to be ‘realy’ private. I don’t wont
to use something like this:
someThing.prototype.someLocalvariable =

or

function someThing() {
   this.someLocalvariable = '';
}

or attaching function inside someThing() like this:

function someThing() {
      var someLocalvariable = '';
   this.getLocalvariable = function() {
      return someLocalvariable;
   }
}

I would be very grateful for any guidance and assistance.

  • 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-27T13:42:12+00:00Added an answer on May 27, 2026 at 1:42 pm

    Your last example of what you don’t want to do won’t work (it has syntax errors), (it’s been fixed) but I think you may have meant the usual way of doing this, which is to make the getter and setter closures within the constructor function (below).

    Unfortunately, if you want truly private variables, this is just about your only option. There is no other way to get truly private, instance-specific variables. However, see “hack” below.

    Here’s the correct version of the usual way of doing this (which I think you said you don’t want, but for completeness):

    function SomeThing() {
        var privateVar;
    
        this.setPrivateVar = function(val) {
            privateVar = val;
        };
        this.getPrivateVar = function() {
            return privateVar;
        };
    }
    
    // use:
    var t = new Something();
    t.setPrivateVar("foo");
    console.log(t.getPrivateVar()); // "foo"
    

    Like most, I first read of this pattern on Douglas Crockford’s site.

    This option does carry a downside: Every instance created via the SomeThing constructor function gets its own two functions. They cannot be shared between instances. So if there are going to be hundreds or thousands of SomeThing instances in your app, that’s something to be considered from a memory perspective. If there are going to be a couple of hundred or fewer, it probably doesn’t matter. (Those numbers are pulled out of a hat and you should not trust them, you’ll have to review your code’s memory use when/if there’s some kind of issue; but you get the idea.)

    The hack: If your instances will already have some kind of unique identifier on them as public data (or you’re willing to add one, again it will be public), and if you’re willing to add a fair bit of complication into the use of the instances, you can have a private cache that holds the data for all of your instances that only your code can access, and key into that cache via the unique identifier of the object. Like this (in this example, I’m allocating the id values, but you can use existing unique IDs if you have them):

    var SomeThing = (function() {
        var cache = {}, idAllocator = 0;
    
        function SomeThing() {
            this.id = ++idAllocator; // The unique identifier, can be a string if desired
            cache[this.id] = {};
        }
        SomeThing.prototype.getPrivateVar = function() {
            var data = cache[this.id];
            return data && data.privateVar;
        };
        SomeThing.prototype.setPrivateVar = function(value) {
            cache[this.id].privateVar = value;
        };
        SomeThing.prototype.destroy = function() {
            delete cache[this.id];
        };
    
        return SomeThing;
    })();
    

    Here’s how that works: All of the functions are closures over the cache local variable in the outer scoping function. We index into that using the unique ID of the object, which gives us an object on which we put our private data members. When the code using the instance is done using it, that code must call destroy (which is a major downside to this pattern) so we remove the private data object from cache by deleting the property for our id.

    Caveats and costs:

    • You still have a public piece of data that is the key to your private data (id in the above)
    • Users of the instances created by SomeThing must call destroy on those instances when they’re done with them. This is anathema to the way JavaScript’s garbage handling works, but it’s a requirement of the pattern above because otherwise you end up with cruft building up in the cache object.
    • (I wouldn’t worry about this one) Eventually, if you’re using the automatic id values above, you’ll run out of them, if your app creates and destroys a lot of these instances. But JavaScript numbers go very high up indeed, and if that’s an issue just find a different way to allocate IDs rather than the simplistic always-increasing system above.

    I haven’t had to use the pattern above in my work yet, but I expect there are use-cases for it involving thousands of SomeThing instances and thus the desire not to have per-instance functions.


    Side note: In the above, I changed someThing to SomeThing. In JavaScript, the standard practice is for the names of normal functions to start with a lower-case letter, and for the names of constructor functions (ones you use with new) to start with a capital letter. Since SomeThing is meant to be used with new, I capped it. This is only convention, but it’s an overwhelmingly popular one and, of course, it’s used within the language definition itself (Date is a constructor, setHours is a function).

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

Sidebar

Related Questions

given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I have problem when want read object with where (double variable) this is my
I want implement in my software solution an VBA editor but in c# 3.0.
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to implement search functionality for a website (assume it is similar to
I want to implement an automatic update system for a windows application. Right now
I want to implement a paperless filing system and was looking to use WIA
I want to implement in Java a class for handling graph data structures. I
I want to implement a two-pass cache system: The first pass generates a PHP
I want to implement an ISAPI filter like feature using HttpModule in IIS7 running

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.