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
2

3 Answers 3

3
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;
Sign up to request clarification or add additional context in comments.

3 Comments

ok - this makes sense. I guess it goes without asking there is no bareword function. :-) sometimes this language just loves for you to keyboard
What would such a function do? A bareword is a language element, not some kind of special value.
I understand - lame joke on my part
0
var o4 = { 'abc', r1.name() }

this one should be:

var o4 = { 'abc': r1.name() }

2 Comments

right! problem is on the attribute name side. will edit description above
i think cdhowie has the answer-- no dynamic keys in object literal syntax.
0

You could create your own property setter

function setProperty (prop,val,obj) {
    if (typeof prop === "undefined" || typeof val === "undefined") return undefined;
    if (typeof obj === "undefined") obj = {};
    obj[prop] = val;
    return obj;
}

// use 1
var o1 = setProperty(r1.name(),r1);

// use 2
var o2 = {};
setProperty(r1.name(),r1,o2);

2 Comments

good idea, but I think if I were doing this I would write a createObject with arguments arg0=name0, arg1=value1, .... to return an object.
you're using the base object type {} which is different from Object or even x = function (){}. All the above is doing is setting a property, first creating it if it doesn't exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.