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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:09:46+00:00 2026-05-31T03:09:46+00:00

This question is for objects in Javascript. I notice that I can add a

  • 0

This question is for objects in Javascript.

I notice that I can add a string and an Error object just fine, and the Error object text is concatenated with no problems.

try {
    throw new Error('Sample Text');
} catch (error) {
    document.writeln('There was an error. ' + error); // Go plus sign!
}

This outputs There was an error. Error: Sample Text which is pretty cool. The Error object knew what string I wanted to concatenate. My own objects do not act nice like this.

var myObject = (function () {
    var text = 'Text I want to concat.',
        get_text = function () { return text; },
        that = {};
    that.get_text = get_text;
    return that;
}());
document.writeln('What does my object say: ' + myObject); // Uncool

My own object outputs What does my object say: [object Object] and does not act nice like the Error object does.

I do not want [object Object] to be output. How can I change what string myObject spits out when being added to a string?

  • 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-31T03:09:47+00:00Added an answer on May 31, 2026 at 3:09 am

    You should give your objects a toString method that returns the appropriate string. So you just need to rename get_text to toString. You could write it something like this:

    var myObject = (function () {
        var text = 'Text I want to concat.';
        return {
          toString : function () { return text }
        };
    }());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow-up question to Javascript Serialization of Typed Objects . That solution
I see that over on this question LINQy way to check if any objects
The question is this. I have a number of persisted objects that I'll pull
I was messing around with JavaScript, and noticed that this can never be a
I have a JavaScript object that does something like this - using a closure
First let me preface this question by saying that I'm fairly new to Javascript.
I just read this question: are there dictionaries in javascript like python? One of
This question is specifically related to overriding the equals() method for objects with a
I recently asked this question: Expose XML or Objects - thanks all for the
nobody loved my first question about this: Creating Entity Framework objects with Unity for

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.