Stream Of Consciousness While Analysing Code
Our excellent new coachineer Mary Rose Cook did a workshop on closures in Javascript and looking at how we analyse code. We were put in pairs and asked to log our thought process (stream of consciousness style) as we analysed a piece of code.
The observer could not help with the analysis but could only ask for more detail and clarity in the log. It was a lot of fun and a wake-up call as to how much more time I need to spend on my Javascript skills. I’m also not all that nice to myself in my head which I don’t think is helping me. Will also work on being kinder to myself. Acknowledging what you don’t know is a positive thing and I shouldn’t be seeing it as negative.
Here’s the code snippet and below is what I logged going through it all. Please ignore typos.
Code to Analyse
var variableA = 0;
function functionA() {
// Can I access variableA on this line? For example, can I console.log it? Why?
};
functionA();
Analysis Log
Code sample 1
reading through the function to understand what will be retunred
variable is declared and assigned at line 1
function functionA() is declared on line 3
function currently only contains commented out code which will produce nothing
function is invoked on line 7 so function will run when file is run.
going to run without any changes in node
assumption is it will run in node but return nothing
assumption confirmed, ran with no errors and returning nothing
looking at commented out code, can i use variable.
lets see what happens if return variable A on line 4
added “variableA” to line 4 within function(){ }
assumption would be this will be callable as its declared unsure regarding scope
and not confident at all with that so will run again to see.
was thinking if we need to return but it should still give error, no it wont
would just give undefined and without returning we cannot see. in fact would
console.log be better?
will try console.log so we can see in nodei
so we got undefined which would imply that there is an issue. i would guess
scope myself.
in a hacky fashion i’d be tempted to add this prior to the variable if it is in
the global domain. i may have gone off on one though
line 4 now reads “this.variableA;” as hoping this will now call the variable in
the correct scope???????
no
incorrect this is not the correct way to do it. what are my next steps/
investigate scope further?
investigate hoisting perhaps?
question i would google, tyr to formulate that.
ycalling a variable from within a function javascript
i’m a dummy. never returned anything. oh how embarrassing
lets delete the this as that was not corerct and instead lets rertun something,
lets return variable A
added return to start of line 4 and removed this. “return variableA;
lets run node again expecting 0 to be returned when ran
HUZZAH
it was retunred
literally feel so stupid
This is part of my daily blog series while I attend Makers Academy coding bootcamp. If you’d like to read more you can find them here.
Makers Academy Week10 Day6