JavaScript Call Stack
JavaScript में Call Stack एक mechanism है जो functions के execution को manage करता है। Call stack यह track करता है कि कौन-सा function currently execute हो रहा है, और execution के बाद किस function को call किया जाना है। इसे एक stack की तरह imagine किया जा सकता है, जहां functions LIFO (Last In, First Out) order में execute होते हैं।
Call Stack कैसे काम करता है?
Call stack एक stack data structure की तरह काम करता है, जहां functions push होते हैं जब उन्हें call किया जाता है और pop होते हैं जब उनका execution पूरा हो जाता है। जब एक function execute होता है, तो उसे stack में सबसे ऊपर push किया जाता है। जैसे ही उसका execution पूरा होता है, वह stack से pop हो जाता है।
Example: Simple Call Stack Flow
function greet() {
console.log("Hello!");
}
function sayName() {
console.log("My name is John.");
greet(); // Function inside a function
}
function start() {
console.log("Starting...");
sayName(); // Calling another function
}
start();
Call Stack Flow:
- start() function call होता है, तो यह call stack में push होता है।
- start() के अंदर sayName() function को call किया जाता है, इसलिए यह stack में push हो जाता है।
- sayName() के अंदर greet() function को call किया जाता है, इसलिए इसे भी stack में push किया जाता है।
- जब greet() का execution complete होता है, तो यह stack से pop हो जाता है, फिर sayName(), और आखिर में start() भी pop हो जाता है।
Stack Representation:
[ greet() ] // Call greet() first
[ sayName() ] // After greet() completes, return to sayName()
[ start() ] // Finally, return to start()
Real-Life Example (Simple Analogy)
Call Stack को आप इस तरह समझ सकते हैं:
- Imagine कि आप एक रेसिपी बना रहे हैं।
- पहले आप ingredients list check करते हैं (यह पहला function है)।
- फिर आप cooking शुरू करते हैं (दूसरा function)।
- फिर आप serve करते हैं (तीसरा function)।
जब आप cooking कर रहे होते हैं, तो आप ingredients को hold करके रखते हैं। जैसे ही cooking complete होती है, आप serving की तरफ बढ़ते हैं। यही call stack में functions के execution का तरीका है।
Stack Overflow (Call Stack का Overflow)
अगर recursive functions (जैसे कि एक function बार-बार खुद को call करता है) infinite loop में चले जाएं, तो call stack भर जाता है, और इसे Stack Overflow कहते हैं। यह तब होता है जब stack में बहुत अधिक functions push हो जाते हैं और memory limit exceed हो जाती है।
Example: Stack Overflow
function recursive() {
recursive(); // This function calls itself endlessly
}
recursive(); // This will cause a stack overflow error
Key Points to Remember:
- LIFO (Last In, First Out): Call stack हमेशा इस order में काम करता है। जो function last में stack में push होता है, वो सबसे पहले execute होता है।
- Single-threaded: JavaScript एक single-threaded language है, मतलब यह एक समय में केवल एक task को handle करता है। Call stack इसका part है जो execution order को manage करता है।
- Stack Overflow: Recursive calls या बहुत गहरे function calls से stack overflow हो सकता है।
Summary:
JavaScript का call stack functions को execute करने और उन्हें सही sequence में manage करने के लिए use होता है। यह LIFO order में काम करता है, और हर function के execution को track करता है। अगर functions को recursively call किया जाए या बहुत ज्यादा nested functions हों, तो stack overflow हो सकता है।
No comments:
Post a Comment