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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:58:37+00:00 2026-06-01T02:58:37+00:00

should a getter of a private property return a reference to the property or

  • 0

should a getter of a private property return a reference to the property or the value of the property (or a clone if the property is an object).

  • 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-01T02:58:38+00:00Added an answer on June 1, 2026 at 2:58 am

    If the property has a primitive type (string, number, boolean) or a reference to an immutable object (String, Number), the answer is that you return the value/reference directly, since there’s nothing the caller can do with it that will change your object’s state.

    If the property has a reference type to a mutable object (an array, an Object, a Date, lots and lots of other things), it depends on whether you want the caller to be able to change the properties on your object’s copy of it. If you do, return the reference. If not, clone it.

    For example, let’s assume an object with a private array and some operations you can perform on that array:

    var obj = (function() {
        var o = {};
        var theArray = [];
    
        o.addItem = function(item) {
            theArray.push(item);
        };
    
        o.getItemAt = function(index) {
            return theArray[index];
        };
    
        return o;
    })();
    

    (Note that this is not meant to be a glorious example of object design. 🙂 )

    Now you want to add a getArray operation. You can do that in two ways:

    You can return a reference to the array:

    o.getArray = function() {
        return theArray;
    };
    

    …which means the caller can do all sorts of things to it (like remove items from it, an operation the object didn’t previously support) that will change your object’s state (because you’ve given them direct access to the formerly-private array).

    Or you can return a clone of the array:

    o.getArray = function() {
        return theArray.slice(0);
    };
    

    …which means the caller can’t do anything to the private array, they can only operate on the copy of it you gave them.

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

Sidebar

Related Questions

Suppose I have a class with a private property and associated public getter and
Should we declare the private fields as volatile if the instanced are used in
Possible Duplicate: Can I create an automatic property (no private member) with get and
According to this answer here: Getter and Setter? The following function should work, however
I was just wondering about why should I use property in a class instead
BlazeDS will not serialize a property unless it has both a getter and a
Every time I create an object that has a collection property I go back
Is it good practice to use the private field, or the property, when writing
Should createUrl be called on controller or in views ? It doesn't matter? Or
Should I constantly have a session open in my web site with user accounts,

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.