Quirk Odd Object - JavaScript - Beginner

I am reading an article that says it {}is a valid JavaScript program.

I tried and it worked fine.

Then I tried this and it worked:

{name:'Lord Stark'} <--- the whole program (without assigning it to a variable or something)


But then I tried the following, and he introduced the error into a comma.

{name:'Lord Stark',reignsOver:'Winterfell'} <--- again this is the whole program


My question is: why does a simple object with more than one property (and therefore a comma) return an error if it is not assigned to a variable, if an object with only one record does not work?

+4
source share
1 answer

{} - an empty block.

{name: 'Lord Stark'} is a block with a label and a string (which will do nothing).

{name: 'Lord Stark', reignsOver: 'Winterfell'} - , , , , , undefined reignsOver, , .

{} , var x = {name: 'Lord Stark', reignsOver: 'Winterfell'};.

, smarts , , {a: 1, b:2} "" . , - , if (1) {name: 'Lord Stark', reignsOver: 'Winterfell'}.

, , :

, , . , JavaScript. , , , .

, JS- {} - , .

:
http://www.ecma-international.org/ecma-262/6.0/#sec-block
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/block
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/label

+7

All Articles