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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:22:52+00:00 2026-05-23T22:22:52+00:00

A lot of properties in my objects may be either a value or a

  • 0

A lot of properties in my objects may be either a value or a function that returns a value. Accessing a value is different to calling a function (foo; vs foo();). I currently take the following approach. Can anyone recommend anything better?

var foo = {
    bar: function(){
        return 1;
    },
    // bar: 1, // bar may also be a standard value
    GetBar: function(){
        return $.isFunction(this.bar) ? this.bar() : this.bar;
    },
    Get: function(property){
        return $.isFunction(property) ? property() : property;
    }
}
foo.GetBar();//1
foo.Get(foo.bar);//1

I’m currently using the foo.GetBar(); approach, but because I then have to write a a new function for every property than van be either a value or a function, I am thinking that it is best to go with the foo.Get(foo.bar); approach to keep it DRY. If you cannot recommend a better solution, I would still like to hear your feedback on both of the supplied methods.

Thanks.

  • 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-23T22:22:53+00:00Added an answer on May 23, 2026 at 10:22 pm

    I generally use a global utility in this case to turn every property that might be a function into one, then always call the function instead of accessing the property:

    function functor(v) {
        return typeof v == "function" ? v : function() { return v; };
    };
    
    unknown = functor(unknown);
    var myVar = unknown();
    

    It looks a little silly in that example, but it’s easy enough to incorporate it into an object like yours:

    var foo = {
        bar: "some value or function",
        get: function(key) {
            return functor(this[key])();
        }
    };
    

    I usually use this approach for configuration variables that might be values or functions – in that case I call functor on the way in rather than the way out:

    function Foo(height, width) {
        this.height = functor(height);
        this.width = functor(width);
    }
    
    // make a new object with a fixed height and a width dependent
    // on the current window size
    var foo = new Foo(10, function() { return $(window).width() - 100; } );
    // now always assume functions
    console.log(foo.height(), foo.width());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a GAE app that has objects with a lot of boolean properties.
We have a a large set of objects that include composition and name properties,
Our code uses a lot of system properties eg, 'java.io.tmpdir', 'user.home', 'user.name' etc. We
A lot of the time I will have a Business object that has a
A lot of programming languages and frameworks do/allow/require something that I can't seem to
I'm looking for a good way to add arbitrary properties to the objects in
So I've created a series of objects that interact with a piece of hardware
I've done a lot of serialization development lately, mostly for sending objects over sockets,
I have an ICollection with objects: private ObservableCollection<ViewItem> items; The viewItems have no properties.
In my design I am using objects that evaluate a data record. The constructor

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.