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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:27:22+00:00 2026-05-24T11:27:22+00:00

when constructing an object using methods from other objects as an attribute name get

  • 0

when constructing an object using methods from other objects as an attribute name get Syntax Error: Unexpected token . – cannot find correct syntax

var R = function(a) { this.arg = a; };
R.prototype.name = function() { return this.arg; }

var r1 = new R('abc');
var name1 = r1.name();        // => "abc"

var o1 = { 'abc': r1 }        // this works with constant
var o2 = { name1: r1 }        // does not work with variable (see answer)
var o3 = { r1.name(): r1 }    // this fails - syntax
var o4 = { 'abc': r1.name() } // this works

have tried { (r1.name()): r1 }, but that fails as well.


please note that strings and integers are evaluated as barewords whereas methods and variables are not:

var o5 = { e.1: 123 }          // fails
var o6 = { 'e.1': 123 }        // succeeds
var o7 = { 1: 123 }            // succeeds
var o8 = { '1': 123 }          // same as o7
  • 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-24T11:27:23+00:00Added an answer on May 24, 2026 at 11:27 am
    var o2 = { name1: r1 }        // this works with variable
    

    This has the same meaning as:

    var o2 = { 'name1': r1 }
    

    In other words, it’s still treating name1 as a literal string. The key in an object literal must be a constant or a so-called “bareword” — this is a restriction of the language. Variables and function invocations cannot be used as keys in object literals (but they can be used as values). Using variables will appear to work, but the variable name will be used as the key and not the variable’s value.

    You will have to do something like:

    var o2 = {};
    o2[name1] = r1;
    
    var o3 = {};
    o3[r1.name()] = r1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given: Constructing an ADO Connection object from one thread and giving it to another
I'm trying to read an object from a XML file using XMLDecoder. The construction
Is there a way to mock object construction using JMock in Java? For example,
Where I work, people mostly think that objects are best initialised using C++-style construction
I'm constructing a method to take in an ArrayList(presumably full of objects) and then
Could you please tip how to properly name object construction approach where public proprieties
Using jQuery what provides for faster object construction: With the HTML code $('<dd class=foo
From the MSDN documentation for the FileInfo.Name property, I see that the data for
For various reasons I'm constructing a C# lambda dynamically using the expression tree facilities.
Many C++ programmers have suffered from the fierce clashes with the global C++ objects

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.