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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:34:55+00:00 2026-05-25T01:34:55+00:00

I want to create getter/setter methods dyanmically to retrieve private properties. This is what

  • 0

I want to create getter/setter methods dyanmically to retrieve private properties.

This is what I did.

First of all, I made the class:

function winClass (posX, posY, w, h) {
  var x = posX || 0;
  var y = posY || 0;
  var width = w || 0;
  var height = h || 0;
}

Then I extended winClass with getter/setter methods, as follows:

winClass.prototype.getX = function () {
  return x;
}

winClass.prototype.setX = function (val) {
  x = val;
}

And then I tested:

var win1 = new winClass (10, 10, 100, 100);
document.write (win1.getX ());

But the following error comes when I try to setup the ‘getX’ method: ‘x is not defined’.
It makes sense because that ‘x’ isn’t in the winClass scope but thus I don’t know how to setup dynamically getter/setter methods for private variables.

Any ideas?

  • 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-25T01:34:55+00:00Added an answer on May 25, 2026 at 1:34 am

    The getter/setters have to be in the scope that can see the private variables and the only scope that can see these variables is the internals of the constructor. That’s why these variables are actually private. So, to make setters/getters for them, you have to put the functions in that scope that can see them. This will work:

    function winClass (posX, posY, w, h) {
      var x = posX || 0;
      var y = posY || 0;
      var width = w || 0;
      var height = h || 0;
    
      this.getX = function() {return(x);}
      this.setX = function(newX) {x = newX;}
    }
    
    var win1 = new winClass (10, 10, 100, 100);
    alert(win1.getX());   // alerts 10
    

    You can see it work here: http://jsfiddle.net/jfriend00/hYps2/.

    If you want a generic getter/setter for privates, you could do it like this:

    function winClass (posX, posY, w, h) {
      var privates = {};
      privates.x = posX || 0;
      privates.y = posY || 0;
      privates.width = w || 0;
      privates.height = h || 0;
    
      this.get = function(item) {return(privates[item]);}
      this.set = function(item, val) {privates[item] = val;}
    }
    
    var win2 = new winClass(10,10,100,100);
    alert(win2.get("x"));    // alerts 10
    

    And, if you want to hack around the private nature of these variables which makes no sense to me (as you might as well make them standard instance variables then), you can do it like this:

    function winClass (posX, posY, w, h) {
      var privates = {};
      privates.x = posX || 0;
      privates.y = posY || 0;
      privates.width = w || 0;
      privates.height = h || 0;
    
      this.getPrivates = function() {return(privates);}
    }
    
    winClass.prototype.getX = function() {
        return(this.getPrivates().x);
    }
    
    winClass.prototype.setX = function(newX) {
        this.getPrivates().x = newX;
    }
    

    Example here: http://jsfiddle.net/jfriend00/EKHFh/.

    Of course, this ruins the private nature of the variables so there isn’t really any point in doing it this way as making them regular instance variables would be easier and have the same access control.

    And, for completeness, here’s the normal instance variable method that freely lets you add accessor methods to the prototype, but the variables aren’t private.

    function winClass (posX, posY, w, h) {
      this.x = posX || 0;
      this.y = posY || 0;
      this.width = w || 0;
      this.height = h || 0;
    }
    
    winClass.prototype.getX = function() {
        return(this.x);
    }
    
    winClass.prototype.setX = function(newX) {
        this.x = newX;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can create getter and setter for an int member. How to do this
I want to create a class with a nested enum. public class Foo {
I want to create conditional comments in XSLT. But when I use this: <!--
I want to create a class file dynamically. Here it goes... With the given
Summary Code sample: Class People { // private property. private $name; // other methods
The getter and setter class is becoming NULL when i try to fetch values
I want create a excel with Apache POI in java and I must insert
i want create image animation , i have 50 images with png format now
I want create module which update list of usb devices automatically (not only mass
We want to create a widget platform for a particular website. I couldn't find

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.