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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:55:36+00:00 2026-06-13T12:55:36+00:00

Can someone explain what is happening in the code below? I’d expect toString to

  • 0

Can someone explain what is happening in the code below? I’d expect toString to get called for either both foo and bar, or neither. How is literal object notation different from adding fields to an object after it is created?

function Obj(v) {
    this.v = v;    
};

Obj.prototype.toString= function() {
    window.alert("to string called for " +
                this.v);
    return this.v.toString();
}        

var foo = new Obj('foo');
var bar = new Obj('bar');

// toString is not called here.
var map = {foo : 'blah'};
// toString is called here.
map[bar] = "blah2";

Why do object literals not use toString() while adding to an existing object does use toString()?

http://jsfiddle.net/pByGJ/2/

  • 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-13T12:55:37+00:00Added an answer on June 13, 2026 at 12:55 pm

    The main reason that object literals don’t evaluate the identifier to the left of the colon is so you’re not force to quote all literal names (as you do in JSON).

    Bracket notation forces you to quote property names, if you don’t, it will be evaluated as a variable.

    The reason toString() does get called in the second example is because bar has to be converted to a string to be used as a property name.

    In your first example, you’re just creating a literal object (that is the exactly the same as {"foo" : 'blah'}). So that is never using the variable foo

    If you want to create an object using a variable name, you can’t use literal object notation, you have to use [] which is what forces it to call toString()

    Here’s a function to create objects with variable names in one expression.

    function obj(key, value /*, key, value, ... */) {
        var obj = {};
        for (var i = 0, ln = arguments.length ; i < ln; i+=2) {
            obj[arguments[i]] = arguments[i+1];
        }
        return obj;
    }
    

    Clearer Example

    The fact that your variable names and values are the same doesn’t help understanding the problem. Let me suggest this code

    var foo = new Obj('fooValue');
    var bar = new Obj('barValue');
    
    var map = {foo : 'blah'};
    map[bar] = "blah2";
    
    // You expect map to be {fooValue: 'blah', barValue: 'blah2'}
    // But it's {foo: 'blah', barValue: 'blah2'}
    

    To do what you need, use my obj function

    // Almost as clear as literal notation ???
    var map = obj(
        foo, 'blah',
        bar, 'blah2'
    );
    // map = {fooValue: 'blah', barValue: 'blah2'} Yay!!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can some one please explain what is happening in the code below and how
Can someone give me an essential Wordpress Loop and explain to me what's happening
Can someone explain the threading model in windows metro? I really get confusion about
Can someone explain what exactly is happening at a low level / memory management
Can someone explain this simple, yet deceiving, anomaly? There are two models, where B
Can someone explain me why this works: select val1, val2, count(case when table2.someID in
Can someone explain why, in this situation, scandir is getting my directory, but the
Can someone explain step by step how this query is processed? SELECT F.ID, F.FirstName,
Can someone explain why it's best use is for prototyping? Should it not be
Can someone explain how the View and ViewModel are connected? I can't find anywhere

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.