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
Clique para expandir
Clique para explorar o mapa mental interativo completo
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
Clique em qualquer texto ou marcação de tempo para ir diretamente àquele momento do vídeo
Compartilhar:
A maioria das transcrições fica pronta em menos de 5 segundos
Cópia com um cliqueMais de 125 idiomasPesquisar no conteúdoIr para marcações de tempo
Cole a URL do YouTube
Insira o link de qualquer vídeo do YouTube para obter a transcrição completa
Formulário de extração de transcrição
A maioria das transcrições fica pronta em menos de 5 segundos
Instale nossa extensão para Chrome
Obtenha transcrições na hora sem sair do YouTube. Instale nossa extensão para Chrome e acesse com um clique a transcrição de qualquer vídeo direto na página de reprodução.