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

  • Home
  • SEARCH
  • 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 8605109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:44:36+00:00 2026-06-12T02:44:36+00:00

Take the following piece of javascript that is trying to implement a class: function

  • 0

Take the following piece of javascript that is trying to implement a class:

function aClass(){
  this.prop1 = this.getProp1();
}

aClass.prototype.getProp1 = function(){
  // do some stuff
  return result;
}

I can then use it like so:

var obj = new aClass();
alert(obj.prop1);

What I now want to do is this:

obj.prop1 = "some value";

This, of course overrides prop1, and I can no longer retrieve the generated result from the getProp1 function.

So in short, is it possible to create a property that allows read and write, but instead calls a function in both cases for, say, validation purposes?

I’m happy for a completely different approach to what I have here as long as I get the desired effect.

It needs to work with older browsers.

  • 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-12T02:44:37+00:00Added an answer on June 12, 2026 at 2:44 am

    You can define getters and setters for object properties; see here and here. The syntax is going to be identical to getting or setting a property, but code will run instead of a dumb value change.

    Live example.

    Compatibility constraint: will not work in IE < 9.

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

Sidebar

Related Questions

I'm trying to write a piece of code that will do the following: Take
Take the following example plugin: (function($) { $.fn.alertOnClick = function(text) { return this.each(function(){ $(this).click(alert(text));
I don't understand why this is occurring. Take the following piece of psuedo code:
My issue is that sometimes a piece of JavaScript (often Google Analytics) may take
Take the following method (written in Ruby but this question could be applied to
Take the following example, I have a class public class SomeItem { public string
The following piece of code tries to accompolish this. The code loops forever and
I was looking at a piece of code today and notice that this particular
I'm a JavaScript beginner and managed to piece together to following code. It gets
The following piece of code attempts to create a map that shows the minimum

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.