Comments (52)
Great article!
I am a software developer from China, I like your articles very much, I want to translate them into Chinese to help more developers.
Before we translate it into Chinese and publish it on our website, I
want to ask for your permission first! This translation version is
provided for informational purposes only, and will not be used for any
commercial purpose.
In exchange, we will put the English title and link at the end of
Chinese article. If our readers want to read more about this, he/she can
click back to your website.
Thanks a lot, hope to get your help. Any more question, please let me
know.
from dmitripavlutin.com-comments.
Another amazing article, thank you so much!
from dmitripavlutin.com-comments.
After paying for courses, watching a lot of YouTube videos and reading a lot of articles. Finally, this article lays it out very simply. Thanks Dmitri
from dmitripavlutin.com-comments.
Great article! Thank you for your contribution to this world. I think you're very talented!
from dmitripavlutin.com-comments.
Thank you very much for your explanation. What is remain not clear for me is "the lexical context" Is it the same as scope?
Please mores practice examples of closure
from dmitripavlutin.com-comments.
That's awesome!! Thank you so much.
from dmitripavlutin.com-comments.
That's awesome!! Thank you so much.
Glad you like it @Jocelyn59435.
from dmitripavlutin.com-comments.
Great article! Very well laid-out explanation. Thank you!
from dmitripavlutin.com-comments.
Great article! Very well laid-out explanation. Thank you!
You're welcome @Plunky33!
from dmitripavlutin.com-comments.
Great explanation. Keep up the good job.
from dmitripavlutin.com-comments.
Great explanation. Keep up the good job.
Thanks @duc168!
from dmitripavlutin.com-comments.
Thank you very much for the explanation. When you use "in other words" it helps a lot!
from dmitripavlutin.com-comments.
When you use "in other words" it helps a lot!
The idea is to show a different point of view. Glad you find it useful @jrmaiafs!
from dmitripavlutin.com-comments.
Thank you for your well written and very concise article.
P. S.
I think there is one small stepping stone missing in the continuity of your explanation (imho).
"Let’s make a change: innerFunc() to be invoked outside of its lexical scope (outside of outerFunc()). Would innerFunc() still be able to access outerVar?"
You propose to show to how access the inner function from the outside context.
This should have led to an error.
The vital concept of returning the inner function is introduced without an explanation as to WHY.
"Now innerFunc() is executed outside of its lexical scope"
I hope i can make myself understandable. This is just meant as a suggestion.
from dmitripavlutin.com-comments.
Really an awesome explanation ! Thank you so much :)
from dmitripavlutin.com-comments.
Really an awesome explanation ! Thank you so much :)
You're welcome @pavithra0116!
from dmitripavlutin.com-comments.
I made a lot of progress thanks to your simple and precise explanations.
2 comments :
- at the end of "3.The Lexical Scope", a small typo : "The lexical scope of innerFunc() consists of func() and global scope." innerFunc should be replaced by innerOfFunc.
- My real question :
Near the middle of "4.The Closure", you say : "Now innerFunc() is executed outside of its lexical scope.". I don't understand. I think innerFunc() is now executed in the global scope, which is, like you told in my first comment, part of the lexical scope of innerFunc. Can you explain that ?
from dmitripavlutin.com-comments.
@Steph638 Your comments are correct, including the question about lexical scope and global scope. I need to update the post.
from dmitripavlutin.com-comments.
Well explained Dimitri
from dmitripavlutin.com-comments.
Well explained Dimitri
Thanks Marcell (@masiucd)!
from dmitripavlutin.com-comments.
The explanations are pretty clear until you get into the built-in functions because some of the information is implicit. handleClick captures countClicked because countClicked is in global scope or some other scope that encases handleClick that isn't explicitly shown? And myText.innerText is also global and thus is in a parent scope of handleClick? Am I getting that right? Unfortunately Event Handlers are usually the first exposure people learning JavaScript have to closures but also the one that is more difficult to explain IMO. Thanks for your explanation.
from dmitripavlutin.com-comments.
What if the multiply() function in example in 5.3 were changed as follows:
function multiply(a) {
return function executeMultiply(b) {
return a * b * Math.random();
}
}
Would double and triple close over whatever Math.random() returned when double and triple were defined?
from dmitripavlutin.com-comments.
What if the multiply() function in example in 5.3 were changed as follows:
function multiply(a) {
return function executeMultiply(b) {
return a * b * Math.random();
}
}Would double and triple close over whatever Math.random() returned when double and triple were defined?
Nope, Math.random()
would execute when calculating the final result only.
from dmitripavlutin.com-comments.
under the hood,how implements closure?Is it copy the outter function‘s value into the inner function when lexing time?
from dmitripavlutin.com-comments.
Dmitry, thank you for this post. I think that will be a great thing if you write more about javascript and functional programing in your new posts. Thanks!
from dmitripavlutin.com-comments.
Thanks, Dimitri, for your thorough and accessible explanations. I now feel much more confident that I can explain and use closures and currying. Scope and lexical scope are definitely the key to understanding these concepts. This is the first of several articles about closures which I have read that includes this and it made all the difference.
from dmitripavlutin.com-comments.
Thanks, Dimitri, for your thorough and accessible explanations. I now feel much more confident that I can explain and use closures and currying. Scope and lexical scope are definitely the key to understanding these concepts. This is the first of several articles about closures which I have read that includes this and it made all the difference.
That's great @devolasvegas!
from dmitripavlutin.com-comments.
thanks a lot Dmitri for this great article. I am preparing for an interview.. your article is really helping me.
from dmitripavlutin.com-comments.
thanks a lot Dmitri for this great article. I am preparing for an interview.. your article is really helping me.
Good luck in your interview @mohanzeal! I hope my post will help you.
from dmitripavlutin.com-comments.
"Now innerFunc() is executed outside of its lexical scope"
Is it really? It's executed in global scope. And global scope is inside all functions' lexical scope, isn't that correct?
from dmitripavlutin.com-comments.
"Now innerFunc() is executed outside of its lexical scope" Is it really? It's executed in global scope. And global scope is inside all functions' lexical scope, isn't that correct?
Thanks @Kreynar and @Steph638 for finding the problem that innerFunc()
executes inside of the global scope (i.e. its lexical scope). The post has been fixed in this regards.
from dmitripavlutin.com-comments.
amazing content, learned alot from it.
from dmitripavlutin.com-comments.
amazing content, learned alot from it.
Glad you find it useful @bilalyaqoob!
from dmitripavlutin.com-comments.
Excellent. Now I understood exactly what closure means. Thanks a lot.
from dmitripavlutin.com-comments.
Excellent. Now I understood exactly what closure means. Thanks a lot.
You're welcome @jaynaras!
from dmitripavlutin.com-comments.
That's a very useful article about closures.
from dmitripavlutin.com-comments.
That's a very useful article about closures.
Thanks @awadhesh31st! Glad you like it. :)
from dmitripavlutin.com-comments.
What a clear and concise explanation! thank you.
from dmitripavlutin.com-comments.
What a clear and concise explanation! thank you.
You're welcome @kwu-work!
from dmitripavlutin.com-comments.
Thank you. This is very insightful.
from dmitripavlutin.com-comments.
Thank you. This is very insightful.
Thanks @heraldjose10!
from dmitripavlutin.com-comments.
Thank you ! This is a very useful article.
" The closure is a function that accesses its lexical scope even executed outside of its lexical scope."
But how setTimeout function execute outside of its lexical scope?
Thanks for your explanation.
from dmitripavlutin.com-comments.
hey man thank you for the wonderful set of articles.
from dmitripavlutin.com-comments.
@varadan13 You're welcome!
from dmitripavlutin.com-comments.
I really appreciate how clear the topic is in your work. I also learned how currying works and your example made that very clear in a small amount of space. Good explanations of closures, lexical scope, and currying while maintaining a simple enough context for examples!
from dmitripavlutin.com-comments.
@danyn Glad you like it 😄 .
from dmitripavlutin.com-comments.
@xuxuewen Thanks! Sure, feel free to translate. 👍
from dmitripavlutin.com-comments.
Can you please explain how the following code works? 🙏🙏
for(var i = 0; i < 3; i++) {
const log = () => {
console.log(i)
}
setTimeout(log, 1000)
}
This code prints: 3, 3, 3 to the console but, As soon as, I change the keyword var to let the same code is printing 0, 1, 2 on to the console.
from dmitripavlutin.com-comments.
Can you please explain how the following code works? 🙏🙏
Hi @shankar7042! Please see a detailed explanation here.
from dmitripavlutin.com-comments.
Thank you for your explanation. It's really helpful
from dmitripavlutin.com-comments.
Thank you for your explanation. It's really helpful
@AmirovQodir You're welcome. 👍
from dmitripavlutin.com-comments.
Thanks a lot! Really helpful. But stack.length = 0; isn't does nothing it deletes all values inside the array.
from dmitripavlutin.com-comments.
Related Issues (20)
- react-usememo-hook/ HOT 12
- javascript-json-modules/ HOT 7
- javascript-import-meta/ HOT 3
- javascript-array-group/ HOT 16
- javascript-array-flatmap/ HOT 23
- interface-vs-implementation/ HOT 11
- vue-next-tick/ HOT 4
- props-destructure-vue-composition/ HOT 7
- ref-reactive-differences-vue/ HOT 5
- environment-variables-javascript/ HOT 3
- ref-in-vue/ HOT 1
- typescript-function-type/ HOT 5
- vue-v-model-form-inputs/ HOT 2
- react-forwardref/ HOT 4
- javascript-enum/ HOT 19
- react-comments/ HOT 8
- typescript-record/ HOT 10
- screen-window-page-sizes/ HOT 3
- javascript-pure-function/ HOT 1
- Great article
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dmitripavlutin.com-comments.