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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:59:25+00:00 2026-05-28T14:59:25+00:00

I currently have the following javascript code (loosely modeled after how a similar objective

  • 0

I currently have the following javascript code (loosely modeled after how a similar objective is achieved in should.js):

var example = function() {
    var isFoo, isBar;

    return {

        baz: function(x) {
            if (isFoo) return x + 2;
            else if (isBar) return x - 2;
        },

        get foo () {
            isFoo = true;
            return this;
        },

        get bar () {
            isBar = true;
            return this;
        },
    }
};

This allows me to do something like:

example().foo.baz(2);
// 4

My primary objective is to allow foo and bar to be called without requiring parenthesis since they will never take any arguments themselves and makes chaining together calls a lot more readable. When I passed this code through uglify.js it choked on the get keyword and when I investigated I found that using the get keyword isn’t really a great idea (both for performance and compatibility).

Any thoughts on how I could refactor this piece of code so that I can continue to call foo and bar without parenthesis and without resorting to using the get keyword? I’ve tried defining foo and bar as properties but couldn’t figure out how to make it work.

  • 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-28T14:59:26+00:00Added an answer on May 28, 2026 at 2:59 pm

    You could use __defineGetter__, but that’s not a terribly great idea either. Firefox has some new metaprogramming apis, but this is unusable in any other browser.

    The baseline is that if you want your code to run in old browsers, this can’t be done.

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

Sidebar

Related Questions

I currently have the following js code function clearMulti(option) { var i; var select
I currently have the following javascript array: var stuffs = ['a', 'b']; I pass
I currently have the following Javascript, and I am not sure what I should
I'm currently doing the following to give my javascript code a namespace: (function(foo, $,
currently I have the following code: String select = qry.substring(select .length(),qry2.indexOf( from )); String[]
I currently have code that does the following: private final static ExecutorService pool =
We currently have an ant task that contains something similar to the following: <filelist
I have the following code: <a class=button href=javascript:ShowHide('elaborate_1') style=font-size:24px; padding:0 10px; margin-left:10px;>COLLAPSE</a> <div id=elaborate_1
I'm working on separating JavaScript code from my HTML. I currently have code that
I'm trying to create a simple binaryStreamReader in javascript. Currently I have the following:

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.