{} – Object Literals in Javascript

{} is object literal syntax.
let say
const circle = {};

Above circle is returning an object.

Object in Javascript is essentially collection of key-value pair.

const circle = {
radius: 1,
location: {
x: 1,
y: 1
},


This content originally appeared on DEV Community and was authored by DEV Community

{} is object literal syntax.
let say
const circle = {};

Above circle is returning an object.

Object in Javascript is essentially collection of key-value pair.

const circle = {
    radius: 1,
    location: {
       x: 1,
       y: 1
    },
    draw: function() {
     console.log('draw');
    }
 };

This circle object have 3 members: radius, location and draw

If a member is

  • function then it refers as method(), so draw is method.
  • Other members are called as properties.

Now, you can access the member using the . notation like:
circle.draw() and it will output, draw in console.

So, this is most commonly used method of defining an object.
You, can also define an object using factories and constructors.

How factories and constructors works and what is the other way of accessing the object will be explained in next blog.

Follow @msabir to keep yourself updated.

Cheers.


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-03-07T05:08:58+00:00) {} – Object Literals in Javascript. Retrieved from https://www.scien.cx/2022/03/07/object-literals-in-javascript/

MLA
" » {} – Object Literals in Javascript." DEV Community | Sciencx - Monday March 7, 2022, https://www.scien.cx/2022/03/07/object-literals-in-javascript/
HARVARD
DEV Community | Sciencx Monday March 7, 2022 » {} – Object Literals in Javascript., viewed ,<https://www.scien.cx/2022/03/07/object-literals-in-javascript/>
VANCOUVER
DEV Community | Sciencx - » {} – Object Literals in Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/07/object-literals-in-javascript/
CHICAGO
" » {} – Object Literals in Javascript." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/03/07/object-literals-in-javascript/
IEEE
" » {} – Object Literals in Javascript." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/03/07/object-literals-in-javascript/. [Accessed: ]
rf:citation
» {} – Object Literals in Javascript | DEV Community | Sciencx | https://www.scien.cx/2022/03/07/object-literals-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.