All language subtitles for 2. Introducing Constructor Functions

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:02,150 --> 00:00:04,730 Let's start with constructor functions and for this, 2 00:00:04,760 --> 00:00:11,540 I have a brand new project, empty HTML file which only imports app.js because in this module, we'll 3 00:00:11,540 --> 00:00:14,180 not build any kind of fancy app, 4 00:00:14,180 --> 00:00:20,750 instead we'll work with the things behind classes since we already saw how we can use classes in 5 00:00:20,750 --> 00:00:21,770 a real project 6 00:00:21,800 --> 00:00:29,030 in the last module. So we can create a class, person for example as you learned, there you can add a field 7 00:00:29,420 --> 00:00:35,440 name for example which will be translated to a property when you create an object based on the class. 8 00:00:35,570 --> 00:00:41,480 You can have a constructor in here where you could for example also add a property like this and you 9 00:00:41,480 --> 00:00:43,820 can also have a method in here where you 10 00:00:43,820 --> 00:00:48,890 for example console log Hi, I 11 00:00:49,220 --> 00:00:58,720 am, then you add this.name and I am this.age years old, 12 00:00:58,720 --> 00:01:02,140 of course we could have used string interpolation here as well. 13 00:01:02,140 --> 00:01:04,700 This is something very basic which we did before 14 00:01:04,810 --> 00:01:10,240 and then of course we can instantiate this as often as we want by calling new, executing person as a 15 00:01:10,240 --> 00:01:11,290 function 16 00:01:11,290 --> 00:01:12,640 and here we go. 17 00:01:12,640 --> 00:01:16,420 You could also accept an argument in the constructor and use that in there, 18 00:01:16,420 --> 00:01:23,000 here I'm working with hardcoded values only and therefore let's see what we get when we call greet here, 19 00:01:23,050 --> 00:01:29,320 save everything and then load this page and indeed we see Hi, I am Max and I am 30 years old. 20 00:01:29,350 --> 00:01:31,060 This is nothing new, 21 00:01:31,090 --> 00:01:37,480 this is how you use classes and this is what we learned about in the last course module. Now behind the 22 00:01:37,480 --> 00:01:45,130 scenes, classes utilize a concept that has been around in Javascript forever basically, for a very 23 00:01:45,130 --> 00:01:46,330 long time 24 00:01:46,330 --> 00:01:49,120 and that's the concept of constructor functions 25 00:01:49,120 --> 00:01:53,070 and of course we have a constructor function, a constructor method in the class 26 00:01:53,110 --> 00:01:58,840 and as you will see, that is basically related to the concept of constructor functions. In the past 27 00:01:58,840 --> 00:02:04,960 in Javascript and still today in many scripts you see out there or in scripts that need to run like 28 00:02:04,960 --> 00:02:07,960 that in older browsers which don't support classes, 29 00:02:07,960 --> 00:02:14,260 you will see constructor functions instead of classes being used. Now constructor functions are a special 30 00:02:14,260 --> 00:02:17,890 type of function that acts as a blueprint for objects, 31 00:02:17,890 --> 00:02:25,180 same as class does, that can hold and set up properties and methods just like a class and that can then 32 00:02:25,180 --> 00:02:28,150 be created with the new keyword. 33 00:02:28,150 --> 00:02:34,120 So of course in modern browsers and modern scripts, we typically work with a class but behind the scenes, 34 00:02:34,300 --> 00:02:42,070 this class here would essentially be written as a function with the function keyword, with any name of 35 00:02:42,070 --> 00:02:47,830 your choice in this case person where still the convention is to use a capital character to make it 36 00:02:47,830 --> 00:02:53,200 clear that this function should not be called as a normal function but together with the new keyword 37 00:02:53,290 --> 00:02:59,620 to be used as a constructor function because indeed with the new keyword, Javascript will call this function 38 00:02:59,620 --> 00:03:01,600 differently than it normally would 39 00:03:01,600 --> 00:03:06,940 and then other than that, you write this as a regular function and to replicate this class, you would 40 00:03:06,940 --> 00:03:10,220 have to set this age equal to 30 here, 41 00:03:10,270 --> 00:03:19,660 this name equal to Max and this greet equal to another function, an anonymous function here, which holds 42 00:03:19,810 --> 00:03:21,760 that logic. By the way, 43 00:03:21,820 --> 00:03:27,430 if you are a little bit more experienced with Javascript, you'll know that this here, assigning name and 44 00:03:27,430 --> 00:03:32,620 greet like this is not exactly equivalent to name and greet set up like this here in the class 45 00:03:32,620 --> 00:03:37,310 but for now it's the only way we can set it up and it will for now have the same effect, 46 00:03:37,330 --> 00:03:41,290 I'll dive into how this is different and how it could be done in a different way 47 00:03:41,380 --> 00:03:42,680 later in this module. 48 00:03:42,700 --> 00:03:48,100 So for now this would give us a person constructor function which we can execute to build such a person 49 00:03:48,100 --> 00:03:54,850 object and indeed, that code down there can stay as it is and if we then go back and reload this page, 50 00:03:54,880 --> 00:03:59,380 we got the exact same output as before but now using a constructor function. 5550

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.