Table of Contents
hide
Closure in Javascript Hindi
closure एक inner function होता है जो outer function के variables को access कर सकता है |
एक function के अन्दर जो दूसरा function define होता है उसे inner function केहतें हैं | inner function, parent यानि outer function के variables और parameters को access कर सकता है | इसी concept को closure कहा जाता है |
उदाहरण:
<!DOCTYPE html> <html> <head> <title>Closure in Javascript</title> </head> <body> <h3>Closure Example</h3> <script> function outer(x) { const y = 20; function inner() { document.write(`outer function's addition result is: ${x+y} `); } inner(); } outer(10); </script> </body> </html>
output
उदाहरण में देखिये हमने एक nested function बनाया है | outer function के अन्दर inner function को define किया है | outer function में एक variable declare किया है और एक parameter pass किया है |
inner function में variable और parameter दोनों की values को access करके उसका addition किया है और result को display किया है |
आशा है आपको javascript में closure क्या है समझ में आया होगा |