Object Literal
A JavaScript object literal is a comma-separated list of name-value pairs wrapped in curly braces. Object literals encapsulate data, enclosing it in a tidy package.
an example object literal:
1 | var myObject = { |
Object literal property values can be of any data type, including array literals, functions, and nested object literals.
Object Literal Syntax
Object literals are defined using the following syntax rules:
- A colon separates property name from value.
- A comma separates each name-value pair from the next.
- There should be no comma after the last name-value pair.
several ways how to create object
Objects can be initialized using new Object()
, Object.create()
, or using the literal notation (initializer notation).
1 | // Using the Object() constructor: |
1 | // Using Object.create() method: |
1 | // Using the bracket's syntactig sugar: |
1 | // Using a function constructor |
1 | // Using the function constructor + prototype: |
1 | // Using ES6 class syntax: |
1 | // Singleton pattern: |