Now let me get rid of that existing code,
you find a snapshot of the code we wrote thus far attached to the last lecture and
let me write some new code.
An important thing you work with or important data structure you work with in javascript are objects.
You could have something like the person object and you create an object typically with curly braces
and then in there, you have key value pairs.
You define the key by simply typing, well whichever key you want to use, let's say name
then you have a colon, you must have the colon and after the colon,
you have your value,
in my case a string Max and then with a comma, you can add another key value pair,
age 29.
Now this is an object and I can log person here, if I now execute play.js, we see that object being
printed here.
Now objects allow us to group data together,
you can also not just have variables in there so to say but you can also have functions in there,
for example a greet function and there you can simply have an anonymous arrow function or an arrow function
general as a value after the colon and then in there, you could have console log
Hi I am
and then the name and this would now be accessed with the this keyword, instead of an object you have
to use this to
typically and there is something special about this which I showed in that video I linked before
in an earlier lecture
but typically this refers to the surrounding object
and now we can use the dot notation,
so dot to access properties or methods,
so variables or functions inside of that object,
so we could now use this name.
And now I can use that person and call greet with the dot notation and executing it as a function,
so the dot notation is important on this which refers to the object or on you, referring to that object
which is stored in this constant here. And now if I execute this again, I'm saying hey I'm undefined,
now why is that?
Well the reason for that indeed is what I talked here about arrow functions.
This now here refers to the global scope, to the global node runtime scope and not to this object, to
have it refer to that,
we either have to use the old school function, like this. Now if I repeat that, I'm saying Hey I'm Max or
we use a slightly different approach or syntax here in an object
and that is you don't use an arrow function but you use greet like that.
So you omit the colon, add the parentheses after the key name and then without a function keyword or
anything like that, you add your function body
and now this is a method of this object
and you see
Hi I'm Max too now.
So this is something important and this is the syntax you'll see me use throughout this course, without the colon
parentheses right after the key and then the function body.