Home >Web Front-end >JS Tutorial >Introduction to the Object type of JavaScript_javascript skills
There are two ways to create Object instances. The first is to use the new operator followed by the Object constructor, as shown below:
Another way is to use object literal notation. Object literals are a shorthand form of object definitions that are intended to simplify the process of creating objects with a large number of properties. The code looks like this:
When defining an object through an object literal, the Object constructor will not actually be called.
Generally speaking, dot notation is used when accessing object properties, but square bracket notation can also be used to access object properties in JavaScript. When using square bracket syntax, the attribute to be accessed should be placed in the form of a string within square brackets, as follows:
There is no difference between the functions of the two, but the main advantage of the square bracket syntax is that you can access attributes through variables:
If the attribute name contains characters that will cause syntax errors, or the attribute name uses keywords or reserved words, you can also use square bracket notation, for example:
In general, it is recommended to use dot notation unless square bracket notation is necessary.