These are chat archives for CodeHubOrg/discussions

9th
Mar 2017
Tim Handy
@TimHandy
Mar 09 2017 07:59
var foo = 1;
function bar() {
    foo = 10;
    return;
    function foo() {}
}
bar();
alert(foo);
Is this a trick question? I expected that when bar is called on line 7 it would reset the global foo (line 1) to 10 as inside the bar function the var keyword wasn't used (ie. not a local variable). I expected the bar function call to have no output (which it didn't), but then the alert on line 8 should have output "10". I don't get it?
Tim Handy
@TimHandy
Mar 09 2017 08:05
hmm, it's the line 5 empty foo function declaration that screws it. If I comment that out I get what I expected. I didn't even expect that function to do anything, 1) because it's after the return keyword, and 2) because it's an empty function. Must be witchcraft.
Dave Thomson
@rinse0ut
Mar 09 2017 08:08
Looks like it’s to do with scope and closures.
Tim Handy
@TimHandy
Mar 09 2017 08:38
urghh. thanks, I'll file that under 'trick questions above my paygrade' for now.
Dave Thomson
@rinse0ut
Mar 09 2017 08:41
I think it’s because js vars are passed by value and not by reference.
Tim Handy
@TimHandy
Mar 09 2017 08:48
so I know that primitives are pass by value and objects are pass by reference, but haven't the foggiest of how the mechanism for the example relates to that. I'll move along.
Have a go at this guys, this'll make you cry: http://davidshariff.com/quiz
I don't have my exact pass % because I had to restart the quiz so some of the answers I had remembered.
Dave Thomson
@rinse0ut
Mar 09 2017 08:51
As far as I understand, I may be wrong but if foo was passed by reference it would behave as you expected and modify the original outer scoped foo, but as it’s passed by value, it’s value is only changed in the scope of the function.
Dave Webb
@cranbim
Mar 09 2017 08:56
Voice from the shadows here. was just catching up (though I have missed soooo much), and happened to see Tim's question. Inside bar, the function definition for foo gets hoisted to the top of the bar scope (and I'm guessing creates a local variable=function) before the line that sets foo to 10. that line is then modifying the hoisted local foo, leaving the global foo unchanged. I think.
Jack Leigh
@leighman
Mar 09 2017 08:59
Yeh, if you remove the function foo line you can see it works as you expect. The function foo is still parsed, even though it's after the return so creates an inner foo that is shadowing the outer foo and is modified instead of the outer one
Dave Webb
@cranbim
Mar 09 2017 09:02
Hoisting is useful, meaning you can put your (classic) function definitions anywhere in the code, and rely on them being in the right place at runtime. But it does not seem to work for arrow functions or const myFunc=function(){} style declarations. Nor for this.myFunc=function(){} and that gets me in to trouble, because I am a terribly unfashionable OOP coder!
Tim Handy
@TimHandy
Mar 09 2017 09:02
@leighman @cranbim @rinse0ut Ahh, now I understand. Yep it gets hoisted, and it's that version of foo that gets reassigned to 10, leaving the outer foo untouched. Of course. that makes sense now. Thanks guys.
I knew these things on their own but hadn't been able to think it through and connect the dots.
@cranbim yep ES6 declarations are not hoisted.. i read that recently: update: Fail. I'm wrong... see Jack's response below.
Jack Leigh
@leighman
Mar 09 2017 09:05
arrow function style still gets hoisted but if you assign it to a var. const won't get hoisted
Dave Webb
@cranbim
Mar 09 2017 09:06
OK. Good to know. Thanks
Tim Handy
@TimHandy
Mar 09 2017 09:06
ahh, didn't know that. thanks Jack.
Dave Thomson
@rinse0ut
Mar 09 2017 09:29
As.far
Katja Durrani
@katjad
Mar 09 2017 23:43
JS101 site has a holding page now (check it out javascript101.co.uk apparently there's some Easter eggs - thanks @rinse0ut), but we have a staging site too where we will publish latest changes - staging.javascript101.co.uk There's something on about and also resources pages now :) Thanks for that, too! Deploying with Git works a charm. If anybody interested in setup ask me at next session
Dave Thomson
@rinse0ut
Mar 09 2017 23:56
Nice one Katja :+1:
I’ve created a google doc to prepare and discuss the content for the site:
Please add content, ideas and comments there, thanks!