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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:55:51+00:00 2026-06-14T10:55:51+00:00

I have a contrived example in javascript to explain what I am trying to

  • 0

I have a contrived example in javascript to explain what I am trying to do:

I have an object:

var Item = {
  _first = undefined,
  _second = undefined,
  whole = putTogether()
};

function putTogether() {
  if (_first && _second) 
    return _first + '_' + _second;

  return '{ Invalid Values }';
}

I am trying to be able to access Item.whole as a property. Is there a way I can do this so that putTogether is evaluated every time it is accessed, rather than initially when the object is created?

I am aware I can define an anonymous function for Item.whole, but I am specifically trying to construct it so that it can be referenced as a value rather than a function.

  • 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-14T10:55:53+00:00Added an answer on June 14, 2026 at 10:55 am

    You may do this :

    var Item = {
        _first : undefined,
        _second : undefined
    };
    
    Object.defineProperty(Item, "whole", {
        get : function(){
            if (this._first && this._second)  return this._first + '_' + this._second;
            return '{ Invalid Values }';
        },
    });
    
    console.log(Item.whole); // prints  { Invalid Values }
    Item._first = "a";
    Item._second = "b";
    console.log(Item.whole); // prints a_b 
    

    Demonstration

    ​
    MDN reference of defineProperty

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

Sidebar

Related Questions

Let's say I have the following contrived example: var allListItems = $('li'); ... doSomeStuff();
In python, you can have a function return multiple values. Here's a contrived example:
I have a page (contrived example below) with a Google doc and a download
In this very contrived example, I have an array with 3 elements that I'm
I have two models that are related to each other. For a contrived example,
This is a contrived example, but lets say I have declared objects: CustomObj fooObj;
This is a very contrived example as it's not easy to explain the context
Forgiving the contrived example, if I have... class Condiment def ketchup(quantity) puts adding #{quantity}
I have a WTL 8.0 SDI application for Windows Mobile 5. In this contrived
I have a string of HTML contained in a var called content. The string

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.