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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:13:14+00:00 2026-06-07T06:13:14+00:00

I am trying to achieve something like the following using OO JavaScript: class Sample

  • 0

I am trying to achieve something like the following using OO JavaScript:

class Sample
{
  public int x {get; set;}
  public int y {get; set;}

  public int z
  {
    get {return x+y;}
  }
}

I could not understand on how to implement property ‘z’ in above class.

  • 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-07T06:13:15+00:00Added an answer on June 7, 2026 at 6:13 am

    You have to use a function. As of ECMAScript 5th edition (ES5), that function can be a “getter” for a property that you access in the normal non-function way; prior to that you have to use an explicit function call.

    Here’s the ES5 way, using defineProperty: Live copy | source

    function Sample()
    {
        // Optionally set this.x and this.y here
    
        // Define the z property
        Object.defineProperty(this, "z", {
            get: function() {
                return this.x + this.y;
            }
        });
    }
    

    Usage:

    var s = new Sample();
    s.x = 3;
    s.y = 4;
    console.log(s.z); // "7"
    

    With ES3 (e.g., earlier versions):

    function Sample()
    {
        // Optionally set this.x and this.y here
    }
    Sample.prototype.getZ = function() {
        return this.x + this.y;
    };
    

    Usage:

    var s = new Sample();
    s.x = 3;
    s.y = 4;
    console.log(s.getZ()); // "7"
    

    Note that you have to actually make the function call getZ(), whereas ES5 makes it possible to make it a property access (just z).


    Note that JavaScript doesn’t (yet) have a class feature (although it’s a reserved word and one is coming). You can do classes of objects via constructor functions and prototypes, as JavaScript is a prototypical language. (Well, it’s a bit of a hybrid.) If you start getting into hierarchies, there starts to be some important, repetitive plumbing. See this other answer here on Stack Overflow for more about that.

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

Sidebar

Related Questions

I am currently trying to achieve something like this: Based on this class, I
What I am trying to achieve is something like this: class object: def __init__(self):
I am trying to achieve something like the following but dont know whats wrong:
I am trying to achieve something like this. The Expandable List consists of the
I am trying to achieve exactly something like this - a text view on
im trying to achieve something but i dont really know how I have set
I'm using Maven 3.0.3 and trying to achieve the following: project-a (this is my
I'm trying to generate an Html.ActionLink with the following viewmodel: public class SearchModel {
I'm trying to pass my View an instance of the following ViewModel: public class
I am trying to achieve something like this with CSS: I'd like to keep

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.