The problem with the global scope
Before we can confront the actual problem in the global scope we need to first build out the core application that is going to be used throughout this chapter. In this lecture we are going to be working in the global scope developing a sudo chat application between the user and the computer. By the end of this lesson, we will have a built application that has some issues that call for a design pattern to rescue us.
Video Transcript
00:00This lecture is the beginning of our process of working with design patterns
00:06And really this section as a whole is really talking about the issue with working directly with the global scope
00:12So although we're not going to solve that problem in this lecture in this lecture
00:16We're actually going to create the problem
00:18We're going to be building a fake chat application that is freely using the global scope without caring about it too much
00:25This lecture is really the setup for the rest of the section
00:27So let's jump right into it and start playing and messing around with the global scope
00:33So let's get right into it
00:34So what I'm going to do is I'm going to create a fake chat a cyber chat where I could then feed in through the console
00:40the conversation
00:42To do that just go ahead into your source files and in your source files
00:46You're going to find in a script there and even before I do that. Let me just say that I've
00:51Imported jQuery just to make my life easier. So we won't have to worry about anything related to the
00:57Dom itself and we're only going to focus on design patterns in a script file
01:02You'll find the starting script and with the starting script
01:05We're going to start over where we have a list of different variables that I pre-configured and I want to go through them really quickly
01:12The idea is to generate some sort of a chat conversation that will happen inside of this cyber chat area to do that
01:19We want to have two heroes
01:20We want to have us the user that will feed in live data
01:24And we want to have computer generated conversation
01:27So in here you will see that I've created a lead self which will start me anytime I say something
01:32It'll start with the words me anytime the computer speaks. We're gonna call it PC and then put whatever the computer said
01:40The a said will basically save all of our conversation the list of the history of all of our conversation a message
01:47Yes would be an option whenever we want to hear yes, and there will be yes
01:50It's a great idea or a message. No will be no. This is a really bad idea a mistake
01:57now a
01:58Sassy stuff and sassy I believe is with s y there as soon sassy
02:03so a sassy stuff is just a list of very sassy stuff that you could pick yourself of
02:08Different answers that the computer might suggest and really this kind of implies to us that we're gonna have your a few different
02:13Functions one which will be our talk function, and I'm gonna create it really quickly
02:19Which is gonna accept a message and then it's gonna do something with that message
02:23The next function that we're gonna have is gonna be a function which is called reply. Yes, no
02:30In other words we're gonna leave the computer the power to decide if it's gonna say yes or no
02:37So we're gonna have to add here some logic for that
02:39The next method or function that we're gonna be creating is gonna be in our case. It's a function
02:44We're gonna say sassy stuff and our say sassy stuff is we're gonna send in any message
02:50Oh, and by the way, so this doesn't have a message, but we're gonna generate a message based on the sassy stuff
02:55That's inside of our sassy slug area there
03:00so those are the three public methods that we're gonna be using but there's also one private method or a method that we
03:05Don't want to give users access to and this private method would be the echo itself and the echo function
03:12really what it does or what it would do is it would accept the message and
03:16Let me just scroll down here a bit it would accept the message and when it receives that message
03:21It will go ahead and register it inside of the a said once registered
03:26It would also go ahead and then print it out printing out only whatever is valid at that given time
03:32So let's start with the echo method and because this is really the heart of the application
03:36We're gonna revisit it a few times
03:38first of all what we want to do is we want to push inside the data and
03:42What I'm gonna do is every copy that comes through I'm gonna wrap it into a div layer
03:47Which is basically gonna make sure that it's gonna break a line and also make sure that I could create specific designs
03:52That would be appropriate if I chose to do so for each message
03:57Alright, so now we have here the message. It's wrapped up inside of a div. The next step would be to probably
04:03Added on to our HTML area, but I'm gonna go ahead here and before we even add it to the chat area
04:09I'm just gonna put just just the last thing that was said I'm gonna put it inside of this talk ID
04:14Which is literally just this area up here change me with JavaScript that's written in it
04:19So I'm gonna go ahead there and just into my script itself
04:22I'm gonna use jQuery and I'm gonna select that area which is inside of the talk
04:28Area and the span that is within I'm gonna go ahead and just change that text to be directly the message
04:36Now if I just go ahead here and I just save this go back into my application and click on refresh
04:41Even though my method is supposedly private it isn't because everything is public which is an issue which is going to something
04:47We're gonna have to address. Hello world
04:49And if I send the echo hello world using my console, we'll see that my up
04:53My copy is gonna update it. Hello world 2 and my copy will update again. All right
04:59So now that we have that basic configuration of the echo
05:03It's time for us to start working on our talk function, which is relatively simple
05:07All we really want to do inside of the talk method is literally just call the echo but send in that lead self
05:15Just before so that way the copy that would lead to the message would be
05:20That actual lead itself than me
05:22so if I go ahead here and save this go back and click on refresh this time around if I call the talk method and
05:28I just send your something such as hello you
05:33The message will be me. Hello you and we could see that maybe would be fitting to put you a bit of a space in between
05:39Automatically, so just gonna make sure that there's a small space there and let's continue the talk method was easy
05:45The reply yes, no is a bit more complex and the reply is no I'm gonna just use here a
05:52Mechanism to basically set my configure my message
05:54What I really want to do is I or want to send the yes or the no and I want it to be random
05:59So I'm gonna go ahead here and just ask from the math a random value
06:03And I'm gonna ask if that value is larger than 0.5 if yes
06:08I'm gonna go ahead there and send that message. Yes, if no, I'm gonna send the message. No
06:13It really doesn't matter the order but really what we're doing here is a ternary operation
06:18If you don't know what a ternary operation is ternary operation is we define our condition in the first path here in this condition zone
06:25Then there's a question mark if the condition was met then whatever is the expression inside of here would be executed if not
06:33After the call call in the other expression the else would be
06:37Conditioned now this information will just be crossed over into the very it's variable itself making it very clean and easy for us
06:44To then just define it in one line instead of creating a few lines
06:49now that I configured it all that's left for me to do is call that echo and then
06:53Send in again the lead of the computer because this time that is a computer generated message
06:58And we're gonna send in also the message with it that we defined
07:02So there we go. We just created the reply is new
07:04Yes, no before testing it out. Let's go ahead and continue and start creating sassy stuff
07:09Which is a bit more complex now sassy stuff is an array and we really don't know the length of the array
07:15So what we're gonna do because this might change or it might be different
07:18So we're not gonna hard code anything inside of here and again
07:21I'm gonna go ahead and just define a message. I really want to go and grab a sassy variable
07:27So I'm gonna go to a sassy stuff and I want to fetch one item inside of here
07:31And all that's left for me to do is to define the variable here. So I'm just going to use the math dot random and
07:37I'm just going to duplicate that by the length of this array
07:44Now I want to make sure also that this value is rounded
07:47But also not only that it's rounded that it's floored because the value the highest value here would actually be
07:52Bigger than our array because the length itself is one
07:56Larger than the actual last position inside of our array now that we've set that up all that's left for us to do is again
08:02Configure it by sending the echo down to our
08:06Echo, which is basically the same exact echo process. All right. So now that we've got that let's go ahead and test it out
08:12I'm just gonna click here on refresh
08:15We'll see that now if I go ahead here and I'll just click here on clear if I say talk not trace talk
08:21I have your basically hello
08:24The talk will work if I said here the say sassy stuff say sassy stuff. Oops
08:31say
08:33Say sassy stuff and just send it on
08:37We'll get something sassy that is said and each time a completely different sassy thing will be executed last
08:44But not least of course was our reply. Yes. No, and if I call to the reply
08:49Yes, no and just run it will have an answer. Yes and
08:53Yes, and yes and yes, and at some point it will be no as well
08:56Alright, so now that we have the core shell of this ready
08:59All it's left for us to do is integrate it into the chat zone and to do that
09:03I'm gonna go into my echo chamber there and we're already storing and saving what's inside of that a said
09:09But the next step for us is also to go ahead and actually add it into our echoing now because my application here has really
09:17only space for six
09:19Lines of copy
09:20I want to make sure that only six and not more than six would ever be seen at the same time and to do
09:26That I'm just gonna go to my a said and I'm gonna ask for the length of it
09:32But I'm gonna subtract six because we have six lines and really my goal here is to start off always from
09:38The six lines before the current line or five lines before the current line to be more accurate
09:44And I'm gonna make sure that I'm always gonna get a value that is larger than minus
09:49So it's gonna be or zero or that value by wrapping inside of my math max
09:54The next variable that we probably would want to have is we're gonna need an out variable
09:59And I'm gonna set it up first to be empty now that we have the basic shell here
10:04All that's left for me to do is to for loop through this or any loop for that matter
10:08Starting from the start position that we just established and then going through the a said until we reach the length of a said
10:16Now we by the way for efficiency perspective just because we're using the a length more than twice
10:21It would be quite efficient to go ahead and set here also a said length. So a said length
10:28Go ahead here comma it out a calm break a line
10:32That was and I'm just gonna go ahead here and just define that variable setting it with a said length
10:37And now I don't have to refer to a said length, which is an object versus a variable
10:42Which is easier access to our processor making our application a little bit more less processor intensive
10:49Alright, so all that's left for us to do inside of this for loop is we're just gonna go ahead here and update that out with
10:55the value that's inside of a said in position I and that's all we really had to do to configure our
11:03element here and
11:04last but not least is to go ahead once we're done with us all here is to go go ahead and we're gonna go
11:10Ahead and actually push the content and replace the content inside of our advert class here
11:15And I'm gonna go ahead here and just call that advert
11:21And I'm just gonna go ahead here and replace the HTML
11:24With our out HTML male content that we've just created. So let's go ahead and test this out
11:30So I'm not gonna recreate here anything. I'm just gonna use the traces here
11:34And I'm just gonna go ahead here once we cross a certain threshold
11:38Then the copy is gonna get cut and only the specific copy that's right there at that given time
11:43The last six are gonna be shown
11:45So we've created our application and we're done with this core application that we're gonna be using throughout this chapter
11:51In this lesson we were playing around with the global scope and we know and you know that the next step is really to solve
11:57This problem that we've created and the problem we created was we overuse the global scope to solve that
12:04We're gonna use two design patterns
12:05One is the object literal design pattern and the other is the namespace pattern and in the next lecture
12:11We're gonna see how to work with them to reduce our footprint in the global scope
