JS Scope Demonstration
A quick demonstration of how scope works with global and local variables in functions. This was an failed attempt to figure out the code var pbjs=pbjs; since scope works as I thought it did and I still don't know the purpose of that line unless pbjs is also a function or object that comes in from the minimized script. This fiddle doesn't test for that.
by Brian Layman
HTML
<p>A GLOBAL variable can be accessed from any script or function, but becomes local when defined in that function.</p>
<p id="demo"></p>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
JavaScript
var carName = "Volkswagon";
myFunction();
myFunction1();
myFunction2();
myFunction3();
function myFunction() {
document.getElementById("demo").innerHTML =
"I can display the car name: " + carName;
}
function myFunction1() {
var carName = carName;
document.getElementById("demo1").innerHTML =
"I cannot display the car name: " + carName;
}
function myFunction2() {
var carName = carName;
carName = "Herbie";
document.getElementById("demo2").innerHTML =
"I can now display the car name: " + carName;
}
function myFunction3() {
document.getElementById("demo3").innerHTML =
"I can still display the car name: " + carName;
}