JavaScript Tutorial - what is closure?

JavaScript Tutorial - what is closure?
JavaScript Tutorial - what is closure?
Title : JavaScript Tutorial - what is closure?
Duration : 18:59
Channel : WebTunings
Label : JavaScript, closure, function, method, scope, scope chain, variable scope, binding, ecmascript, tutorial
JavaScript Tutorial - what is closure?
JavaScript Tutorial - what is closure?
JavaScript Tutorial - what is closure?
JavaScript Tutorial - what is closure?

F1 function statement has variable x and return F2 defined. After F1 invocation returned function F2 will remember variable x. This is called closure. In the above example,closure is the function object F2 + scope chain associated with the function F2 in which function variables are resolved. F2 can access variable x which was earlier defined in function F1. F2 knows all the surrounding where it was created or returned. function F1(x,y){ var z=5; console.log('x=' +x+ 'y=' +y+ 'z=' +z);} F1(1,2); // x=1 y=2 z=5 F1(1,2) invocation creates an object to store all local variables x,y and z. After that this object is added to scope chain. During invocation function F1 has the reference to object that stores all local variables(local variable holder object) as well as scope chain. In the above example after invocation function F1, local variable holder object and scope chain goes for garbage collection. subsequent code you can follow in the tutorial. ------------------------------------------------------------------------------- Facebook page: https://www.facebook.com/pages/WebTunings/339234242822202 Recommended JavaScript Book: http://www.amazon.com/gp/product/0596805527/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596805527&linkCode=as2&tag=webtuningscom-20 Code snippets: https://github.com/webtunings



Share this

Related Posts

Previous
Next Post »