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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:39:43+00:00 2026-06-08T16:39:43+00:00

Yesterday, I posted about prototypal inheritance and constructors . I finally settled on an

  • 0

Yesterday, I posted about prototypal inheritance and constructors. I finally settled on an approach that keeps the code nice and tidy and leaves the prototype alone at the cost of possibly minor performance impact:

function Card(value) {
    // setValue is a public instance method
    this.setValue = function (val) {
        if (!range[val]) {
            val = drawRandom();
        }

        // value is a public instance variable
        this.value = val;

        return val;
    };

    this.setValue(value);
}

My problem with this approach, however, is that I have to call the setValue method whenever I want to set the value of a Card instance to get the validation. What I’d like to do instead is have a custom setter method. Here’s what I have so far:

function Card(val) {
    // value is a private instance variable
    var value;

    // This is a private instance method
    // It's also self-invoking, but that's beside the point
    (function (x) {
        if (!range[x]) {
            x = drawRandom();
        }

        value = x;
    }(val));

    this.__defineGetter__("value", function () {
        return value;
    });

    // Some code duplication
    this.__defineSetter__("value", function (x) {
        if (!range[x]) {
            return false;
        }

        value = x;

        return x;
    });
}

This works well. Calling var card = new Card() gives me an instance with a random value, and calling card.value = null fails because it’s out of range.

My problem with this, other than the fact that it is obviously much longer, is it seems like I’m duplicating some of the code. It would be nice if the setter method was called along with the constructor. That way, I could eliminate the whole self-invoking private instance method.

  • 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-08T16:39:45+00:00Added an answer on June 8, 2026 at 4:39 pm

    First, you should always set the value with obj.value = newValue, even internally, because it invokes your validation. When in the constructor that means:

    this.value = val;
    

    But that won’t work if you do it before the setter and getter are declared. So move it after so that the setter function will exist when it’s set.


    Working example here: http://jsfiddle.net/8tCjm/4/

    var drawRandom = function () {
        return Math.floor(Math.random() * 3) + 1;
    };
    
    var range = {
        1: 'Ace',
        2: 'Two',
        3: 'Three'
    };
    
    function Card(val) {
        var value;
    
        this.__defineGetter__('value', function () {
            return value;
        });
    
        this.__defineSetter__('value', function (x) {
            if (range[x]) {
                value = x;
            } else {
                value = drawRandom();
            }
    
            return value;
        });
    
        this.value = val;
    };
    
    console.log(new Card(1).value); // 1
    console.log(new Card(2).value); // 2
    console.log(new Card(3).value); // 3
    
    console.log(new Card(987).value); // not 987 (1-3)
    ​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yesterday I posted a question here ( FxCop and Code Analysis Rules ) about
Following on from a question I posted yesterday about GUIs, I have another problem
Yesterday I've posted question about SQLite performance issues ( Terrible performance in Zend Db
I posted a question about keys yesterday, and got very helpful responses. I've been
yesterday, I posted a question about Hom to change a DIV id with a
(hello dr.Nick) :) So I posted a question yesterday about a content loader plugin
I have posted this yesterday, I didn't get the the answers that could solve
Yesterday, I posted an answer to a question that included several (unknown to me
I posted this yesterday on SO, and I received an answer that works great,
I posted a question yesterday regarding issues that I was having with my backpropagating

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.