2.8 class methods ownership
So in the last video, we learned about
classes and on properties and methods.
Here too. Now,
new generation javascript also offers a
different syntax for initialization of
properties and methods. And in fact, it’s this
more modern syntax that I will use in this
courses.
So I want to show it to you here as well. You’ve already
learned that properties look like variables
that you
associate with classes
and objects,methods to functions attached
classes and objects, and you will learn
more about this syntax in which we define
properties in the constructor function.
Now there is a more modern syntax that
avoids us to use this constructor function with
the next generation of javascript, like us
will use it in this course. You can assign
a property directly in your class with
myProperty equal to value. So, you skip the call from
manufacturer function. Behind the scenes, it will always be
transformed
to use functions
builder. But it will be easier to
write this and for the methods, it’s enough
similar.
That’s the old way. I showed it to you in the
last video.
Now the javascript method of the next
generation
consists in using the left syntax where you
define a property. Just imagine a
method as a property that
stores a
function in value form and you find yourself,
so my method is equal to, then a function of
arrow. The
great advantage of this syntax is that, since you
use a
arrow function as property value here, the
keyword this has no problem. That’s the reason
exact for which
we will use
this syntax here, in the lower right corner,and the
corresponding syntax. the lower left corner
throughout this course.Let me you
show syntax. Let’s go back to jsbin on the project
on which we have already worked with
human and human classes. We can eliminate
the manufacturer
and the keyword this
and just put the gender equal to male and print. The
gender can be equal to an arrow function
where we console log
sex always uses the keyword this here when
we contact the property.
Although you may ignore this arrow to
the moment, we no longer need
of the manufacturer and we no longer need to call
super for this reason Instead, we call
this name without this name: equal to Matthew and sex
equal to woman if you wish and printMyName to a
arrow function equal like this. Now, if
you erase
this and execute this, you will get an error because
it does not recognize the syntax you have
real need
to go to the drop-down menu
where it is indicated javascript and choose ES6
If you do this and press clear
and press run, you will see Matthew and
a woman.
You will still see the arrows to the left but you
can ignore them. This is the javascript syntax
of the next generation that we will use
in this course.