r/learnjavascript • • Mar 18 '21

JavaScript objects explained: Object literals

https://youtube.com/watch?v=QB_oKKb4L3c&feature=share
168 Upvotes

7 comments sorted by

View all comments

1

u/Al_Maleech_Abaz Mar 19 '21

Nice explanation. Wouldn’t the function here need to have a key associated with it, or is the function name implied as the key?

4

u/3dCodeWorld Mar 19 '21 edited Mar 19 '21

Good question... The code that I used in the example uses a shortcut syntax, which was introduced in ECMAScript 2015. You can also use a key and then a function as a value. If you run the code below in your browser console, both calls would work.

let obj1 = {
   doSomething1(){
      console.log("Hi, from inside doSomething1");
   },
   doSomething2: function(){ console.log("Hi, from inside 
      the doSomething2");
   }
}

obj1.doSomething1();
obj1.doSomething2();

1

u/Al_Maleech_Abaz Mar 19 '21

You know, I used to hate all the syntactic sugar when I first started learning js cuz it all seemed so overwhelming but the more I learn the more i can appreciate it. Nice job, and thanks for explaining this to me. I’m gonna be on the look out for more videos like this.

1

u/3dCodeWorld Mar 19 '21

I find this particular addition quite good... it makes the object literals much more readable.