{} - Object Literals in Javascript
imsabir

imsabir @msabir

About: I am web developer with expertise in cutting edge technologies, technical writer, technical recruiter. I believe in #Gratitude. ✨✨✨

Location:
Somewhere on Earth, Universe 🌎
Joined:
Feb 22, 2022

{} - Object Literals in Javascript

Publish Date: Mar 7 '22
21 11

{} 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.

Comments 11 total

  • imsabir
    imsabirMar 7, 2022

    Stay tune for next update!!

  • Bernd Wechner
    Bernd WechnerMar 7, 2022

    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) .

    • imsabir
      imsabirMar 7, 2022

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

      • Bernd Wechner
        Bernd WechnerMar 7, 2022

        Do you have any examples of such changing terminology?

        • imsabir
          imsabirMar 7, 2022

          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.

          • Bernd Wechner
            Bernd WechnerMar 7, 2022

            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.

            • imsabir
              imsabirMar 8, 2022

              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.

              • Bernd Wechner
                Bernd WechnerMar 8, 2022

                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?

                • imsabir
                  imsabirMar 8, 2022

                  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!!

  • CuriousDev
    CuriousDevMar 7, 2022

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

    • imsabir
      imsabirMar 7, 2022

      Yes, will give you the example for same soon.

Add comment