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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:04:58+00:00 2026-06-14T21:04:58+00:00

function Example(){ var id; }; Example.prototype.getId = function(){ // return this.id; }; Example.prototype.init =

  • 0
function Example(){
     var id;
};

Example.prototype.getId = function(){
     // return this.id; 
};

Example.prototype.init = function(){
   $.post( 'generateId.php', {}, function(data){
       // this.id = data; 
   });   
};

How can I access id within these functions?

  • 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-14T21:04:59+00:00Added an answer on June 14, 2026 at 9:04 pm

    It seems that you think “private” variables exist in Javascript. Private variables are only emulated in Javascript through closures, but do not exist as in other languages. In your code, id is only accessible from within your constructor.

    You can keep id private though, and still be able to access it from within your functions, but you’ll have to declare those functions in your constructor as closures to have access to it:

    function Example()
    {
        //private
        var id;
    
        this.getId = function ()
        {
            return id;
        }
    
        this.init = function()
        {
           $.post( 'generateId.php', {}, function(data)
           {
               id = data;
           });   
        };
    };
    

    Another problem is that you’re trying to access this from within an asynchronous callback. In this context (the callback passed to $.post), this is whatever the context of the calling function was, which is probably undefined or the XmlHTTPRequest object.

    If you want to access it, you’ll have to cache the this of you function (from your original code, assuming id is not private):

    Example.prototype.init = function()
    {
       var self = this;
       $.post( 'generateId.php', {}, function(data)
       {
           self.id = data;
       });   
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example: (function() { var proxied = window.eval; window.eval = function() { return proxied.apply(this,
Here is an example: var Box = function() { this.id = generateUniqueId(); }; Box.prototype
How can I bind to the right of the function? Example: var square =
I have seen this example on the web: $('#questionTextArea').each( function() { var $this =
Ok, in my code I have for example, this: $('.follow').click(function(){ var myself = $(this);
Suppose I have some javascript code like this: function Person(){} var pro=Person.prototype; pro[setName]=function(){} pro[setAge]=function(){}
For example, if I have the object: function Website(){ this.images = []; } Website.prototype.getImages
i can reference link with specific id with following code $(document).ready(function(){ $(a#example_link_id).click(function(event){ var url
For example suppose client side I have a JavaScript function function getLocation() { var
We have the following example in node.js var http = require('http'); http.createServer(function(request, response) {

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.