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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:06:16+00:00 2026-06-05T15:06:16+00:00

I have a Class that has a private variable with a public setter/getter function:

  • 0

I have a Class that has a private variable with a public setter/getter function:

function Circle(rad) {
    var r = rad;

    this.radius = function(rad) {
        if(!arguments.length) return r;
        r = rad;
        return this;
    }
}

var shape = new Circle(10);
console.log( shape.radius() ); // 10
shape.r = 50;
console.log( shape.radius() ); // 10

How can I replicate this using Object.prototype? Or, when would I want to use a closure instead of Object.prototype? This is the closest I could come up with, but as you can see, you can change the property directly.

function Circle(r) {
    this.r = r;
}

Circle.prototype.radius = function(r) {
    if(!arguments.length) return this.r;
    this.r = r;
    return this;
};

var shape = new Circle(10);
console.log( shape.radius() ); // 10
shape.r = 50;
console.log( shape.radius() ); // 50
  • 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-05T15:06:19+00:00Added an answer on June 5, 2026 at 3:06 pm

    If you’re going to use the prototype to store an object’s properties, they are accessible from any code that has a reference to the object. It’s impossible to do what you want.

    What many JS devs do is just name private properties with a leading underscore so that others know not to mess with it, but it doesn’t give you any real protection beyond a suggestion

    Reasons to use closure based approach

    • True private variables, be confident that no one will mess with your privates

    Reasons to use prototype

    • Less memory used (no closures for every instance)
    • Easier to debug (properties are visible on the object itself)
    • Allows monkey patching

    Readers: Please edit the answer with reasons for whatever you think is the best solution.

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

Sidebar

Related Questions

I have a class skImage. This class has a private variable (with a public
Inside my class I have a private variable that my methods use which has
Suppose you have a class Dog , that has public class Dog { private
I have created this Pool that has 5 static variables. public class FruitPool extends
I have a class that has a vector member variable, which I fill up
I have a public class which has the following method and instance variable: public
I have a class MyClass that has a static variable. Class implements smth like
I have a base abstract logger class, that has an instance variable that I
I have this class for page titles: class UI { private static $title; public
I have a class that has several member classes as attributes. The constructor of

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.