Question and solution of attribute name quotation marks and no quotation marks when JS declares an object. js Declaration
In general, attribute names can be enclosed by quotation marks or not, and the effect is the same.
Var obj = {name: '', 'age': 1,}; document. write (obj ['name'] + '<br/>'); document. write (obj. age );
The above two lines of code can be correctly executed.
An error is reported only when your attribute name is invalid or strange.
Var obj = {333: 'This will report an error'}; document. write (obj.333 );
An error is reported.
Var obj = {"333": 'This will also report an error'}; document. write (obj.333 );
If the attribute name is a number, it must contain double quotation marks and be accessed using [] square brackets.
Var obj = {"333": 'correct '}; console. log (obj ["333"]);
Conclusion: valid attribute names can be used for both. And [] access;
If the attribute name is a number, it must be enclosed by "" and accessed using [] square brackets.
Summary
The above is a small series of questions about attribute name quotation marks and no quotation marks when declaring objects in JS. I hope this will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in time!