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

The Archive Base Latest Questions

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

Sorry I’m probably being a realy noob here…but: I have the following javascript object:

  • 0

Sorry I’m probably being a realy noob here…but:

I have the following javascript object:

jeeni.TextField = (function(){

    var tagId;

    privateMethod = function(){
        console.log("IN: privateMethod");
    }

    publicMethod = function(){
        console.log("IN: publicMethod: " + this.tagId);
    }

    jeeni.TextField = function(id){
        console.log("Constructor");
        this.tagId = id;
    }

    jeeni.TextField.prototype = {
            constructor: jeeni.TextField,
            foo: publicMethod
    };

    return jeeni.TextField;
 }());

Now when I run the following code I get the corresponding result:

var textField1 = new jeeni.TextField(21); // Outputs: Constructor
textField1.foo();           // Outputs: IN: publicMethod: 21
console.log(textField1.tagId); // Outputs: 21
console.log(textField1.privateMethod); // Outputs: undefined

So my question is why is privateMethod hidden and tagId is not. I want them both to be private scope.

Please help a noob.

Thanks

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

    You mixed up two ways of creating modules. The problem is that the private var tagId is not the same thing as this.tagId

    Let me start with a version that works and is similiar to what I normally do with AMD modules:

    jeeni.TextField = (function(){
    //here you can put a variable common to all instances
      return {
      init:function(id){
        var tagId = id;
        console.log("Constructor");
    
        function privateMethod(){
            console.log("IN: privateMethod");
        }
    
        function publicMethod(){
            console.log("IN: publicMethod: " + tagId);
        }
    
        return {
           foo:publicMethod
        };
      }
    })();
    
    var textField1 = jeeni.TextField.init(21); //creates instance
    textField1.foo();           // Outputs: IN: publicMethod: 21
    console.log(textField1.tagId); // Outputs: undefined
    console.log(textField1.privateMethod); // Outputs: undefined
    

    This has one disadvantage: for every object instance the functions are copied in the memory. That’s the only reason to use prototypes. But if you want private variables, you’ll probably waste some RAM anyway.

    In your code, if you replace this.tagId with just tagId you’ll be using the private variable, but it will be just one, common for all instances.

    I will think about making your code work and edit if I find a way to do that.

    [edit]

    What Stephen did is close to what you expected your code to do. I wouldn’t want to exlain how it works and why to a co-worker though.

    [edit]

    BTW. Take a look at require.js and AMD (module definition)

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

Sidebar

Related Questions

Sorry, I know this is probably a duplicate but having searched for 'python regular
Sorry I'm new here and my code will probably not be properly displayed... how
Sorry if this is stupid question, but I'm new to MATLAB. I have a
Sorry if this is a noob question, but All my code is error-free and
(sorry about the long title) I have a custom object Person, which in turn
Sorry to bother you all here but I am still trying to understand more
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry for my ignorance here, but when I hear the word webserver, I immediately
Sorry gals & guys for a potentially dumb question but I have been looking
sorry for lamer question, but I really could not found subject. I have a

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.