Seems like it works
console.log({name: 'fred', age: 65}.name); //fred
console.log({name: 'fred', age: 65}.age); //65
until I try to use a method
{greet(){console.log('hello');}}.greet(); //Uncaught SyntaxError: unexcted token: '{'
Assigning an identical object literal to a const and then invoking its method works
const greeter = {greet(){console.log('hello');}};
greeter.greet(); //hello
Is such usage not allowed?
CodePudding user response:
You just lack ()
to properly call the anonymous object
({ greet() { console.log('hello'); } }).greet() // hello