I have an object, defined thus:
var obj = {
'title': 'foo',
'id': '123',
'category': 'aaa',
'meta': 'blah blah'
};
I’d like to subset it, however, I discovered some behavior in JS which I don’t understand:
var foo = {obj.title: obj.id}; // doesn't work
var bar = {obj['title']: obj['id']}; // this doesn't either
var baz = {}
baz[obj.title] = obj.id // this works fine.
Because this is how it is defined in ECMAScript 5, Section 11.1.5:
A property name must be either:
obj.titleis neither an identifier name, nor a string or numerical literal. But even if you’d assign the title to another variable before, you see that the production rule does not evaluate the identifier:Just to elaborate on why it is not a valid identifier name: The full stop (
.) belongs in Puncuation, other [Po] category, which is not part of the allowed characters. These are, among others: