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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:38:21+00:00 2026-05-28T15:38:21+00:00

Currently have this code defining getter/setter for an object’s selectedID property. The object is

  • 0

Currently have this code defining getter/setter for an object’s selectedID property. The object is called Components.

_selectedID: null,

get SelectedID() {
    return this._selectedID;
},
set SelectedID(val) {
    if (!isNaN(val) && val != this._selectedID) {
        this._notifyChanged(val);
        this._selectedID = val;
    }
},

While this works in the sense that the browser knows how to handle the javascript generated, Visual Studio states that there are syntax warnings.

This is another way to say the same thing, but lacks the OOPness of the previous:

(EDIT: Apparently I can use this instead of Components here, so ‘lacking OOPness’ isn’t as much of an issue, but still curious if better).

function GetSelectedID() {
    //return Components._selectedID;
    return this._selectedID;
}

function SetSelectedID(val) {
    //if (!isNaN(val) && val != Components._selectedID) {
        //Components._notifyChanged(val);
        //Components._selectedID = val;
    //}

    if (!isNaN(val) && val != this._selectedID) {
        this._notifyChanged(val);
        this._selectedID = val;
    }
}

Object.defineProperty(Components, 'SelectedID', { get: GetSelectedID
                                       , set: SetSelectedID
                                       , configurable: true
                                       , enumerable:   true });

Note that instead of saying this I need to say Components as this is setup outside of the Component’s object constructor. If I try to move the functions into the constructor I get syntax errors.

Does anyone know if it is possible to achieve both? Something like:

SelectedID: function () {
    alert("Getter");
    return this._selectedID;
},

SelectedID: function (val) {
    alert("setter");
    if (!isNaN(val) && val != this._selectedID) {
        this._notifyChanged(val);
        this._selectedID = val;
    }
},

The above code does not work, though. When I say Components.SelectedID the getter alert does not fire.

  • 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-05-28T15:38:22+00:00Added an answer on May 28, 2026 at 3:38 pm

    There is no method overloading in Javascript so you are just redefining SelectedID with the setter.

    If you want to have one method for both, try something like this:

    SelectedID: function() {
        if (arguments.length === 1) {
            var val = arguments[0];
            if (!isNaN(val) && val != this._selectedID) {
                this._notifyChanged(val);
                this._selectedID = val;
            }
        } else {
            return this._selectedID;
        }
    },
    

    I’m not arguing this is a good idea, it is just the way you would achieve what you are trying to do.

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

Sidebar

Related Questions

We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
Using dotnet 2.0. I currently have code like this : DataView dv = new
Is there a better way to do this jquery code? Currently I have to
There is probably is simple fix for this but I currently have code similar
I currently have this code: if (e.KeyChar == (char)8) { } What I would
I currently have this code for populating a list of comments to return: foreach
I currently have this code. function outputCalendarByDateRange($client, $startDate=2011-06-22, $endDate='2011-06-26') I want $startDate and $endDate
I currently have this code: $uri = $_SERVER[REQUEST_URI]; if (preg_match(/smda/, $uri)) { echo whatever;
In my program I have currently a piece of code that looks like this
I have this code: Dim pathString As String = HttpContext.Current.Request.MapPath(Banking.mdb) Dim odbconBanking As New

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.