<script language="JavaScript">
myTickets = new Array();
startPosArr = new Array();
endPosArr = new Array();
selItem;
eee2 = 0;
function setMyTicketList(){
alert("^^ : " + eee2);
}
The following error occurs where the eee2 is alerted in the above code.
Uncaught ReferenceError: eee2 is not defined
But the funny thing is that if you declare the global variable declaration position of eee2 in the first line as below, it will work normally.
eee2 = 0;
myTickets = new Array();
startPosArr = new Array();
endPosArr = new Array();
selItem;
I'm asking because it's a phenomenon that can't happen in other languages, so I'm a little embarrassed. Thank you.
javascript global-variable
First of all, the answer below is for me to write down to study, so I'm informing you that there may be some mistakes. To explain what I understand, it doesn't work because eee2
is not at the end, but because it's behind selItem
, or because you didn't explicitly declare a variable as varselItem;
A problem with a reference error.
ReferenceError Object indicates an error that occurs when referring to a variable that does not exist. (...) ReferenceError occurs when trying to refer to a variable that has never been declared.
What does that exist in JS? It's been declared. Of course, similar to other languages, it's possible to just declare first and then assign a type and a value later. If you just wrote setItem;
, I think you just wanted to declare it that way.
But what does JS refer to? It's about pulling up its existence to the scope of that level, then looking at it and naming it.Called hoisting, JS's default behavior is to first hoist the function, then hoist variables , and if you think "a stranger has already been assigned something," you can see it as a variable and raise it to the extent it can be declared.
So the next code works.
x = 5;
alert(x);
// I'm going to do the same move that you're expecting.
However, the following code does not cause anything other than errors in the browser developer console.
x;
alert(x);
Why? Why are you bringing someone up to the scope of the war who hasn't been assigned and hasn't been declared a variable like varx;
? So JS throws a reference error that means, "Hey, wait a minute, what's this x
?" and stops processing.
Now, let's look at the code you posted. As soon as the code below is loaded, the developer console prints a reference error called selItem is not defined, and when the function is executed, it prints an additional reference error called eee2 is not defined.
selItem;
eee2 = 0;
function setMyTicketList() {
alert("eee2: " + eee2);
}
That's understandable. The setMyTicketList()
function was hoisted, and when I tried to hoist the variables, I suddenly saw selItem
and the entire hoisting was interrupted. So I couldn't hoist to Amen eee2
who was waiting in the back and became undefined
. Therefore, if you call the setMyTicketList()
function later, you will only encounter an error that you do not know what ee2
is.
On the other hand, the code below makes up to the selItem
reference error, but allows the function to be executed itself.
eee2 = 0;
selItem;
function setMyTicketList() {
alert("eee2: " + eee2);
}
Do you understand why? Very coincidentally, fortunately, eee2
exists and is referenced.
Bonus: The following will also work: This is because all three objects in the code below were successfully hoisted. (Like eee2
in the code below, it is not good to just assign a variable without var
or let
, but that's a bit different, so I'll skip it...)
var selItem;
eee2 = 0;
function setMyTicketList() {
alert("eee2: " + eee2);
}
When I was looking for a job interview in the past, I couldn't answer the question asking if I knew hoisting, so I remembered that I was kicked out, so I wrote it down in detail. For more information, let's explore more with demo. Was it helpful?
© 2024 OneMinuteCode. All rights reserved.