Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
In the last section, we used our person class to create a new instance of a person which we assigned
to the variable lowercase P person then on the line.
After that, we set a value to the first name property on that person instance.
At the very end of that section, we had said that it sure seems kind of awkward to have to first declare
the new instance of the person and then set the first name property after that.
It sure seems to me like every person who exists inside of our application should always have a first
name associated with them.
So in this section, we're going to add a little bit of code to make sure that anyone who wants to use
this person class right here has to provide a first name property to the class the instant that the
class is created.
So the instant we call a new person right here.
To do this kind of required setup or required configuration of our class, whenever we create a new
instance of it, we're going to add something called a constructor function.
A constructor function is going to be another function or method added on to this class right here.
This constructor function gets executed automatically any time we create a new instance of the person
class.
And so it makes it a perfect location to do a little bit of initial setup or initialization of our class.
So to just kind of put this into diagram format, here's what's going to go on.
Any time you and I create a new instance of a class, the constructor function is going to be called
with any arguments that we provided.
When we try to create a new instance, that constructor function will then be executed and it's going
to do some initial setup for us.
And then after all, that runs our new instance of the class, then gets returned and then assigned
to this person variable over here.
So let's get to it.
Let's define the constructor on our person class.
To define a constructor, I'm going to add a little bit of space here underneath our field of first
name, and then I'm going to define a new function with a very special name.
So the function name is going to be a person with a capital P like.
So so notice how the name of this function right here is absolutely identical to the name of the class,
because these two names are completely identical in case and spelling.
So they both have a capital P.
This person function right here will be used as the constructor function.
And so this function will be called automatically any time we try to create a new instance of a person.
And again, that makes it a perfect location to do some initial configuration of our instance.
Now, as we were saying just a moment ago, we kind of want to add on a requirement to our application.
We want to say that any time you try to create a person instance, you have to immediately provide a
first name.
In other words, we should not have any people inside of our application who do not have a first name.
So to add in that requirement, we can specify an argument inside this constructor functions argument
list or the set of parentheses right here.
So inside the set of parentheses, I'm going to write out a name like so.
When as soon as I type that in, you'll notice that we get an error appearing on the right hand side
over here and if you click on it, it highlights where we are trying to create that new person instance
up here.
So as soon as we start to add in an argument to a function that means that anywhere we are calling that
function from needs to also provide the correct number and type of arguments as well.
So in order to make sure that every person gets created with a first name, we just had to add in the
argument right here and then, poof, everything else inside of our code base now says, hey, if you're
going to try to create a person, you have to pass along some argument as well.
Now, we'll fix up this kind of new instance creation right here in just a moment.
But first, let's finish off our constructor.
So as soon as someone provides a name property to this constructor function, we probably want to take
that provided name in, assign it to the first name property.
So to do so, we can write out first name equals name like so.
Now, one thing I want to point out here is that my choice of variable names is a little bit unclear.
I provided a variable name or an argument name here of simply name.
And so it's not super clear inside my constructor if this is supposed to be like the last name or is
it supposed to be the first name of the person?
It's not super clear.
And so you might think, well, it would be a little bit more clear if we just wrote out first name.
But then that makes this line of code a little bit less clear because this would technically be first
name and now we are in a state where things are super unclear.
We've got an argument name, a first name, but our property name is first name as well.
So Darte understands that this right here is probably a line of code that you're going to want to write
all the time, it's very common to have constructor arguments with names that are identical to the property
names that you're attempting to sign them to assign them to.
So rather than making you write out some very awkward code like this right here, Darte has a built
in shortcut that kind of takes care of this entire situation for us.
So let's write out this shortcut, and I think you'll be somewhat happy with how this turns out.
So I'm going to take the set of curly braces here.
I'm going to highlight everything in between.
I'm going to delete it all.
And then going to place a semicolon.
After that person function and then to first name right here, I'm going to add on this dot like so.
OK, so let's talk about what's going on here.
So this is some pretty crazy syntax, but here's what's going on now.
Any time we create a new instance of person and pass in a first argument to that creation, Dart is
going to automatically take that first argument in, assign it to the first name property of the instance
that is created.
So normally when we see this set of parentheses after a function name, we think of it as the argument
list, but DART is instead taking this argument list and it's kind of doing a little shortcut for you.
And it's saying, oh, well, since you referred to this argument as this first name, I'm just going
to take whatever argument was passed to this new person when it was created and automatically assign
it to the first name property for you.
Now, I'm using a lot of words to describe this, but it might not be super clear, so let's kind of
complete this example and I think it'll get a little bit more obvious as time goes on.
It's now back up inside of my main function.
I'm going to find the person first name property Simon right here, and I'm going to delete the entire
thing.
And then going to find where we try to create a instance of a person right here and I'm going to make
sure that I pass in my first name property directly to this constructor function.
So to the thing I'm going to add a string of Steven like so.
Now, you'll notice that all the air messages go away, and if I try to run this thing.
Over here in the console, I'll see Stephen appear again.
All right, let's take a quick pause right here, and when we come back, the next section, we're going
to review what we've spoken about with Constructor's and we'll review this little shortcut of property
assignment right here.
So quick break and I'll see you in just a minute.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.