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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:42:46+00:00 2026-06-11T10:42:46+00:00

In Javascript, it seems like using property accessors is not all that common (unlike

  • 0

In Javascript, it seems like using property accessors is not all that common (unlike in other OO languages such as Java for example).

If I have a Person object with a name, defined as

function Person(name) {
   this.name = name;
}

A person’s name is not going to change, but I do want to be able to access it when needed, so I could do something like:

function Person(name) {
   var name = name;
   this.getName = function() {
      return name;
   }
}

Even in a dynamic language, I think the principles of using getters and setters apply the same way they do to statically typed OO languages (e.g. encapsulation, adding validation, restricting access, etc)

This question may get closed as subjective, but I’m curious as to why this behavior doesn’t appear more often (e.g. Java developers would go crazy if everything was public).

Is there a “standard” way to do this in javascript? I’ve seen Object.defineProperty, but not all browsers support that.

  • 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-11T10:42:48+00:00Added an answer on June 11, 2026 at 10:42 am

    Javascript has intercept-able property accessors:

    http://ejohn.org/blog/javascript-getters-and-setters/

    IMHO this is a far better solution to enforce the Uniform Access Principle than Java’s more strict explicit getters, but that is also part of the simplicity and inflexibility of that language (Groovy for instance allows for similar interception).

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

Sidebar

Related Questions

The javascript one seems pretty simple, just localStorage.clear(). Is there anything similar to that
The following javascript code seems to leak memory but I’m not sure why. I’ve
It seems that JavaScript's Date() function can only return local date and time. Is
I am currently learning Object oriented Javascript but it seems that the alert is
Visual studio seems intent that my javascript code is bad, mostly because it has
I want to send a Javascript array to PHP using AJAX. This seems very
I am trying to load this javascript externally, but it seems to not be
Using .text object property in JavaScript can produce unexpected results in IE8 (blank string
seems like an easy question but I can't get this. I have a property
the following javascript function only seems to work when i have the final confirm()

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.