DEV Community

Cover image for {} - Object Literals in Javascript
imsabir
imsabir

Posted on

{} - 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
    },
    draw: function() {
     console.log('draw');
    }
 };
Enter fullscreen mode Exit fullscreen mode

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 method or property 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.

Top comments (11)

Collapse
 
thumbone profile image
Bernd Wechner • Edited

I think this is more elegant:

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

That is to say, methods are implicitly functions you don't need to declare them thusly (the (...args) suffices for JS to recognize this as a method and not a property) .

Collapse
 
msabir profile image
imsabir • Edited

Yes but depending upon where it invooked there technical terminology changes.Thats all. In global level the are similar

Collapse
 
thumbone profile image
Bernd Wechner

Do you have any examples of such changing terminology?

Thread Thread
 
msabir profile image
imsabir

Iykyk A method is a procedure or function in object-oriented programming. A function is a group of reusable code which can be called anywhere in your program. This is self explanatory. In global level they are similar.

Thread Thread
 
thumbone profile image
Bernd Wechner • Edited

I don't follow, sorry. How is there terminology changing? Nothing BTW is self explanatory, it's all jargon, and to suggest jargon is self explanatory seems puzzling to me.

Generally methods are viewed as functions that belong to an object (most typically a class instance, though in JavaScript and some other contexts the basic class of "object" can be used generically too. I have yet to see any changing terminology here.

And Iykyk seems a meaningless answer to a question IMHO. If it can't be answered, I guess my point stands, that I really don't know what you're talking about when it comes to changing terminology based on where you invoke something. A method remains a method as far as I know and doesn't change names based on context. Being a subset of functions though, methods are functions, but functions not necessarily methods. And a method can thus be supplied in any context that requires a function.

Thread Thread
 
msabir profile image
imsabir • Edited

So do you mean that method is function and functions are method both are same thing???

If you read article, its mentioned that
If a member is

  • function then it refers as method(), so draw is method

If you want to say that method are those functions which we don't declare, then its definitely correct.

I am not getting what else you want to suggest or convey.

Thread Thread
 
thumbone profile image
Bernd Wechner

I'm confused. Can you read? I wrote:

A method remains a method as far as I know and doesn't change names based on context. Being a subset of functions though, methods are functions, but functions not necessarily methods.

And I was aksing you about:

depending upon where it invooked there technical terminology changes.

To which you responded unhelfully with "Iykyk". I'm curious what terminology changes depending upon where it is invoked here. I'm confident there exists some samples of that, but I can't think of one right now so I was asking you, as you made the claim. And my take is a method is a method is a function that belongs an object, and functions are not all methods?

Not even sure where all that came from. All i did was suggest a more elgenat terser means of writing the draw() method. Take it or leave it. But yhen you responded:

Yes but depending upon where it invooked there technical terminology changes.Thats all. In global level the are similar

which remains a mysterious repsonse to me. Sorry. I'm curious if you have examples of technical terminology changing depending on where it is invoked?

Thread Thread
 
msabir profile image
imsabir

Okay. There is one more geeky who wants to know this. So, I will extend this blog, so you all get the understanding as well. Okay!!

Collapse
 
curiousdev profile image
CuriousDev

Thank you, it would be interesting to know, how the function can use one of the properties of the object literal.

Collapse
 
msabir profile image
imsabir

Yes, will give you the example for same soon.

Collapse
 
msabir profile image
imsabir

Stay tune for next update!!