There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
For some reason, whenever I switch the Javascript files, I receive an error saying “elem is not defined”. Something similar happened with the previous exercise where it said I was wrong even though my answer was exactly the same as the solution. Is there something wrong with my browser or is it due to the lesson itself?
Barring the use of modules, JS has but one namespace. That means all script loaded will reside in the same defined area of memory. What’s more, script runs when it loads so the script that loads first will have run before the script that follows.
The scripts do not communicate with each other. JS simply has access to the functions and variables of each since they are all in one place, the namespace.
The apt pupil will foresee the potential for collisions of either variables or functions, or both. Bottom line, we really need to predict this and mitigate it. The flipside could be one script may define a global variable that the second script looks for.
Apart from how many scripts we load, then we have the consideration of plug-ins. Think back to the days when we didn’t have frameworks and polyfills. Variable collisions were a definite concern since we didn’t have modules and everything had to play nice in the same room.
<script>
let windowInnerWidth = window.innerWidth
let windowInnerHeight = window.innerHeight
</script>
Any script that loads and runs with a dependency on these variables will be happy to see them present in memory.
One should add that dependency is an Achilles Heel. If you are combining scripts, for whatever reason, then test for the existence before polling.
if (! windowInnerWidth) {
var windowInnerWidth = window.innerWidth
var windowInnerHeight = window.innerHeight
}
See immediately how redundant that is? All the same, it is not something to overlook. Redundancy is how we keep spacecraft running for decades of operation.
edit: var is used to escape block scope declaration
So… basically if another script aka script2 is looking for a variable in specific and the script1 is called before it in the document and loaded… but also happens to have the variable in the global scope that script2 is looking for, then after script2 is loaded and is looking for the variable that script1 happens to have then it will use the variable from the global scope of script1.
Correct, or could Function and Block Scoped variables be found as well by script2 from script1?