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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:54:58+00:00 2026-06-08T11:54:58+00:00

I have this function that returns an object: String.prototype.test = function(a,b){ var ob =

  • 0

I have this function that returns an object:

String.prototype.test = function(a,b){
    var ob = {};
    ob[a] = b;
    return this || ob
}

//usage
"Test".test('hi','hello');

If .hi isnt attached to test, i want it to return the string.

So with that example I would need:

"Test".test('hi','hello').hi;//returns: hello

To work, but then I also need:

"Test".test('hi','hello'); //returns Test

To work, I tried using || in the return but it’s not working. Thanks for the help.

  • 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-08T11:54:59+00:00Added an answer on June 8, 2026 at 11:54 am

    It’s impossible to make the return value depend on what happens with the returned value.

    You could however return a String object with a property hi:

    Don’t do this in any production code, it’s extremely ugly and nobody expects it.

    String.prototype.doStuffThatNobodyExpects = function(a, b) {
        var s = new String(this);
        s[a] = b;
        return s;
    };
    

    Again, don’t do this in any production code, it’s extremely ugly and nobody expects it.

    Demo:

    js> var s = 'Test'.doStuffThatNobodyExpects('hi', 'hello');
    js> print(s);
    Test
    js> print(s.hi);
    hello
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript function: string.prototype.startsWith = function(str) { return (this.indexOf(str) === 0); }
i am trying to prototype the String Object to have a replaceWith Function that
I have this function called ProperCase that takes a string, then converts the first
I have Clojure function that returns a LazySeq. When I run this function from
I have a function in C++ that returns a vector<vector<double> > object. I have
I have a function that returns a shared pointer to an object (it is
Is it safe to rely on Function.prototype.toString to return a string that will parse
I have a function that returns an 8 digit long binary string for given
Consider this JavaScript code snippet: Object.prototype.log = function() { // here, you have a
I have this function that I would like to condense into some iterator. How

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.