Welcome back.
My friends will come to another lecture today where we are going to do is we going to start building
our architecture, creating those three modules that we discussed before.
So here is why we use modular architecture.
I'm not going to module pattern.
I'm not going to say too much because the modular let me just write it module pattern is one of the
most famous patterns in JavaScript in it's widely used.
Second, it is very convenient to use because you didn't capsulize our data.
It helps to preserve how to see it helps us developer to write a clean code to reuse it.
And we can just extend that module and work on that module in future.
Right now the module is great.
Now how are we going to implement it?
We're going to do that through immediately invoked functions.
Y if you remember that after every EFFI invoke when it's been invoked, it will create a new scope and
it will encapsulate the data.
Right now I'm just going to save it.
So open the starter folder and right.
Click here open with live server.
This will give me I'm not going to use, I'm just going to copy the link.
I'm not going to use Firefox, I'm just going to use Google Chrome for a minute.
And what I'm going to do here, I'm just going to open that.
I'm going to page that link and we're going to open it and I'm going to inspect it.
And here are you going to see a lot of.
A lot of things selected context only, right, so there are a lot of errors that are not with connected
with our coding.
Right.
So we finish this one to go open the jails at the jails.
And here we are going to work on our epee functions.
So I'm going to create the first Batur, the first module or controller.
It will be calories controller and that will be immediately bone function.
Remember how we write the functions, write like that and we need to invoke them outside.
Now I'm just going to copy it here.
I'm going to paste two more and I'm going to change.
So we have to put that one the last.
I have a main controller and I do have this app UI controller that we discussed in the previous lecture.
So I do have these three modules in these three modules are going to be enough for this application.
No, I'm just going to do example.
You don't have to write it.
You just can listen to me.
You don't need to write it here because we you can we will delete that later.
Now, I'm just going to create a variable h here.
It will be two thirty three.
I'm just a little bit inrush.
I want you to not fall asleep, get your coffee, get your favorite drink and be focused.
Now here I'm going to create a function.
It will be here born in this function.
We'll just have one parameter in that will be current here.
So here you're going to pass the current here.
For me, the current year is twenty twenty.
Right.
And it will be return current here minus H.
Otherwise if we just do H minus current year it will be negative and that is what we don't want.
Now when the if using Bogot it will be create a new scope with new data encapsulate now these functions.
I will not be able to call it outside.
I'm just going to show you.
So calorie controller diet and let's not do it like this.
It's going to save it.
I'm going to open it here and I'm going to say calories controller that age.
Right.
And you kind of read the property h how cool is this if I just go to the year before?
And put it out here 20, 20 like that, it will be cannot read property, it's undefined.
No, we cannot access things from here because they're private.
But what we can access, we can return an object with a method inside.
Right now, I'm going to create a public method you can create.
If you write it here together with me, you can write any name you want.
This is for testing only.
So here I'm going to return a function that will have one parameter and I will see counsel no calls
a log.
What?
I'm just going to say you're born with the same parameter that I'm passing from here right now.
Let's test this out.
So let's see if I now can use this public method outside.
So this will be open, exposed to our public school.
Let's see.
Calories control.
The public method here really is.
Yeah.
Let's use the 20/20 here if it's going to give me a 1987.
So why is this working?
This is working because remember, we have done closure's so again with the closures with the EPA,
first we create a new school and that returns an object right here it is in this object.
There is a whole information that we want to deal with.
Now, the public metal can use both these private variables in ways that remember, because this function
is being called, it's been finished.
So why why this public method have the access both of these each and year born when they are private?
So the answer's a the answer is simple.
It is because of the closure.
Now, the closures were created in inner function, have access, has access to the variables in the
parameters of the other function.
Right.
Remember that the definition that I gave it to you, that is why this is working and this is powerful.
So the E.V. from anonymous function, from data encapsulation.
That's why we're going to use it now here.
We're going to write some code.
I'm not going to do it.
This is just a bare structure now.
I'm going to do in main controller.
So the main controller, it's going to be our bridge.
Right.
We need to connect to things in here.
I'm going to pass two parameters in those parameters will be the calories controller and APU I control
and we're going to need it.
Slightly different one calories.
Sethe, yet qty are L in the other one will be app you sethe or you can name whatever you want here,
but here we need to invoke the real cop calories controller in the app controller, make sure that you
copy.
Don't get confused so we can use it here or this will work.
If we don't use it because these two are outside, we can just use them right now.
Just to zoom in a little bit.
Right now we can use the app UI controller here and do whatever we want with that app.
You had control over the calories controller even here directly because it's in outer scope.
But we just choose to have to pass into two parameters here in these parameters.
They don't have the same name with this guy here.
Why in future I'm going to just tell you, if you use the same name here, same name here in here,
you have ten lines where you calling some functions here that are public.
And imagine that in future you're going to see calories controller and you're going to say, OK, calories
some controller.
Now, I only have to change it here.
I don't need to go here in the in the in this.
I'm just going to show it to you, for example, calories, control or the public method.
And imagine we have ten methods that are public here with a comma.
You can add more methods.
Right.
So imagine that we have ten methods and we have ten lines here.
Now, if I change the name of this controller, I will have to change those ten lines.
But not this bad boy here.
This bad boy will be the bridge actually.
And we can use use it like that.
Right.
We don't deal with the real name because the real name can be changed to calories some controller in
future.
And now we need to only here do it once or even the app can be changed.
So I hope you understand what I'm trying to do now for the test.
We're going to do something here now because this is a breach.
Let's communicate.
Let's grab this.
Let's we use that's borrowing.
It's called method borrowing.
Let's borrow this method here and we can now use the name.
We can create return here.
We can return something.
Oops.
And let's create another public method for this controller.
We have the public method for this controller, but we don't have it for here.
We need to print it out.
We need to do something we need to be able to access.
So he's going to be public method.
Me, I don't want to be the same name.
Right.
And that will be a function that will receive a year and let's say console log.
Let's log it straight away.
Has a log calories controller.
This variable here that have the access to the public method of the year.
How cool is this now?
We need to fix something here.
I'm just going to we need to return something here.
We not we don't need to control a lot.
So I'm just going to say return.
You're born and we're going to pass the year.
Oops, not like that.
So we need to return it in order to use it here.
So I'm calling this function with, for example, a year here, but I need to call it straight from
here.
Main controller DOT public.
Public method mean, and I'm going to pass it here 20, 21 before I do have
87.
Now, I should have some different year and that is 1988.
How cool is this?
So what is happening now, man controller has its own return that contains a public method in that public
matter is actually going through the public method and passing value to this one.
And this is just returning in.
This method can reuse, as we that we spoke, can use all the private instances and functions from from
this calorie controller.
That is why we use it.
Now, this guy here will not be able to call this year board, right.
Will not be able to do it like that.
And got you're born is not a function.
That is what is happening.
So it can only access the private members.
So I hope you understand what we're trying to do now.
This is all nice.
And then we're going to just delete this.
I don't need this.
If you don't want to, you can create a commons, but this is what we have now.
We do have the structure in architecture.
Now you know why we use Evey's.
You know why we use closure's you know, why we use this model module pattern?
Because it is working in that way that we can have one controller that communicates with both here and
plus inside the function.
We can declare it is a private and public and we can just have the access of the public.
Right, because the public method will use private variables and functions even after this method is
finished.
And this is working because of the closures, the inner function have the access to the variables and
the parameters of the other functions.
That is that is why the public method is working.
Right.
So thank you very much.
I hope you understand this and I will see you in the next one.
Don't quit yet.
Just tag along and we will try to finish you.
If you are here.
Your true blue means that you're great.
You have done a lot and you're invested in yourself.
Invested time.
Now stay until the end and you will after that, reap the rewards.
Thank you very much.