Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I like to think about scope like a cheeseburger. Double with cheese, bacon, lettuce, tomatoes, pickles, and mustard.
- So global scope would be like when you take a bite somewhere in the middle. It doesn’t really matter where you bite, because
- you’ll always have access to all the toppings. Local would be like you pulled a tomatoe out and just took a bite of tomatoe.
- That bite only affects the tomatoe. If I just bite into the tomatoe, the rest of the cheeseburger is relatively unaffected. So running
- something with local scope has fewer effects on the application as a whole.
- Before talking about strict mode, we have to realize that not everyone likes the same cheeseburger. Some might want
- onions instead of pickles. Or maybe there was a miscommunication and you got a veggie burger instead of 80/20 chuck.
- In order to explicitly put in our order to prevent mistakes and get the desired result (not everyone likes a global cheeseburger),
- we need a way to say, “Hey, JS, this is what we want.” That‘s where use strict comes in. In strict mode, variables have to be declared
- before they can be used.
- Use strict is useful because it keeps accidents like that from happening. Variables are much less likely to get accidentally
- reassigned. Kind of like how at BK you can get it your way, with use strict, if something isn’t explicitly declared, then there
- will be an error. “Excuse me, I did not order bacon. I ordered Canadian bacon.”
- So use strict helps with preventing side effects where variables accidentally get changed or reassigned. It forces you to
- think about the variables you are using, and maybe using const instead of let, and vice versa.
- Pure functions are functions that simpy return the same output everytime when given the same input. For an array in Javascript, it's
- like using slice (pure function) vs splice (side effect of changing the original array everytime it's called).
Add Comment
Please, Sign In to add comment