Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Voxera115858yJavascript uses function scoping.
In a normal for loop you have no local scope within the loop and hence no closures. -
AllenII16198y@Voxera traditionally true, though we do now have block scoping as well with the let/const keywords
-
Voxera115858y@AllenII depending on platform but yes.
But that is not a function of the for loop.
And how does block scope work with closures? I actually do mot know if it is the same as function scope. -
AllenII16198y@Voxera to be honest with you, i only started using closures heavily only a few months ago, so i got nothing to personally say if there's any difference. But seeing as closures are formed by creating an inner execution context (function) i guess there shouldn't be much if any difference at all.
Or I'm missing the point entirely somehow, I dunno -
faisal118yIn JS, functions are treated as first class objects. This allows you to return a function or pass it as an argument/parameter into other functions. This means a function could be created in one place and called in another place; closures help us handle possible problems with variable access (scope). Different from context (`this`).
Reading articles is definitely useful, the one linked above seems handy. -
Voxera115858y@faisal
Yes. And after some sleep I also realized that without the function there is no way to create a closure.
Its not just scope that matters.
Only a function allows you to return actual code which is what triggers closure so without a function no closure and back to the original question: a map uses a function to process each element which can trigger closure while a "for" loop does not by it self use a function and does not trigger a closure.
Related Rants
Today I was asked a question on JavaScript regarding the difference between normal `for` loop and a `map` function on an array with regards to closures. Can anyone help me understand? #javascript #interview
undefined
interview
help
javascript