This content explains a common programming error where a variable declared within an event handler (local scope) is inaccessible outside of it, leading to "Not a Number" (NaN) errors and variables not updating. The solution involves declaring variables with global scope at the beginning of the program.
Mind Map
클릭해서 펼치기
클릭해서 인터랙티브 마인드맵 전체 보기
look at this little program it looks
very similar to ones we've already seen
it creates a variable and then assigns
it a value over here then there's a
non-event that allows the variable to
increase by one every time the button is
clicked finally we set the property of
clicks and that just lets us see how
it's changing over time and we've also
got this watcher down here so we can see
how the value of clicks is changing but
when we run it something weird happens
over here we're getting na n that stands
for not a number and down here we're
seeing that clicks isn't changing at all
now the solution of fixing this problem
is pretty easy and for now it's the only
thing you're going to need to do if you
switch over in a text mode this is even
easier you'll see that clicks is being
created here on line one but we're also
trying to recreate it on line three we
go back in the block mode here's the
program how we're used to seeing it and
it's not that different but suddenly a
program runs as we want and we can see
it both here and down here in the watch
panel the reason that this is happening
is because of a concept called scope and
without getting too into it the basic
idea is if you create a variable inside
of a block like on event and in
particular anything that has this
function command which you're going to
learn more about later in this year it
creates a variable that has local scope
local scope means that you can only use
it in the space that it was created and
that means anywhere inside of this on
event most variables we want are going
to have global scope it's best practice
to create variables for now at the
beginning of your program like we did
here on line one while eventually you're
going to want to create some
local variables for now we don't and so
you're going to need to debug programs
the way we just did make sure you're not
using a bar inside of a non event use
your watch panel to see if strange
things are happening like a variable
isn't changing when it looks like it
should and check if you're getting weird
output for example like we got non it
not a number later in the course you'll
learn more how to use this concept but
for now happy debugging and go catch
some of those local variables to remove
텍스트나 타임스탬프를 클릭하면 동영상의 해당 장면으로 바로 이동합니다
공유:
대부분의 자막은 5초 이내에 준비됩니다
원클릭 복사125개 이상의 언어내용 검색타임스탬프로 이동
YouTube URL 붙여넣기
YouTube 동영상 링크를 입력하면 전체 자막을 가져옵니다
자막 추출 양식
대부분의 자막은 5초 이내에 준비됩니다
Chrome 확장 프로그램 설치
YouTube를 떠나지 않고 자막을 즉시 가져오세요. Chrome 확장 프로그램을 설치하면 동영상 시청 페이지에서 바로 자막에 원클릭으로 접근할 수 있습니다.