ﺪﻨﺷﺎﺑ ﺎﯾ ﯽﻠﺤﻣ ﻪﺑ ﻖﻠﻌﺘﻣ ﺪﻨﻧﺍﻮﺗ ﯽﻣ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯼﺎﻫﺮﯿﻐﺘﻣ
.ﺩﺮﮐ (ﯽﺻﻮﺼﺧ) ﯽﻠﺤﻣ ﻥﺪﺷ ﻪﺘﺴﺑ ﺎﺑ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﯽﻠﮐ ﯼﺎﻫﺮﯿﻐﺘﻣ
:ﺪﺷﺎﺑ ﻪﺘﺷﺍﺩ ﯽﺳﺮﺘﺳﺩ ﻦﯾﺍ ﺪﻨﻧﺎﻣ ﻊﺑﺎﺗ ﻞﺧﺍﺩ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ ﻪﻤﻫ ﻪﺑ ﺪﻧﺍ
function myFunction() {
let a = 4;
return a * a;
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>A function can access variables defined inside the function:</p>
<p id="demo"></p>
<script>
myFunction();
function myFunction() {
let a = 4;
document.getElementById("demo").innerHTML = a * a;
}
</script>
</body>
</html>
:ﺪﺷﺎﺑ ﻪﺘﺷﺍﺩ ﯽﺳﺮﺘﺳﺩ ﻦﯾﺍ ﺪﻨﻧﺎﻣ ﻊﺑﺎﺗ ﺝﺭﺎﺧ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ ﻪﺑ ﺪﻧﺍﻮﺗu2
let a = 4;
function myFunction() {
return a * a;
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>A function can access variables defined outside the function:</p>
<p id="demo"></p>
<script>
let a = 4;
myFunction();
function myFunction() {
document.getElementById("demo").innerHTML = a * a;
}
</script>
</body>
</html>
ﺖﺳﺍ ﯽﻧﺎﻬﺟ ﮏﯾ a ،ﺮﺧﺁ ﻝﺎﺜﻣ ﺭﺩ
.ﺪﻧﺭﺍﺩ ﻖﻠﻌﺗ ﻪﺤﻔﺻ ﻪﺑ ﯼﺮﺳﺍﺮﺳ ﯼﺎﻫﺮﯿﻐﺘﻣ ،ﺏﻭ ﻪﺤﻔﺻ ﮏﯾ ﺭﺩ
.(ﺩﺍﺩ ﺮﯿﯿﻐﺗ ﻭ) ﺩﺮﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻪﺤﻔﺻ ﺭﺩ ﺮﮕﯾﺩ ﯼﺎﻫ ﺖﭙﯾﺮﮑﺳﺍ ﻪﻤﻫ ﻂﺳﻮﺗ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﯼﺮﺳﺍﺮﺳ ﯼﺎﻫﺮﯿﻐ
ﺖﺳﺍ ﯽﻠﺤﻣ ﮏﯾ a ،ﻝﻭﺍ ﻝﺎﺜﻣ ﺭﺩ
ﯽﺗ ﯼﺁ .ﺩﻮﺷ ﻩﺩﺎﻔﺘﺳﺍ ﺖﺳﺍ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﻥﺁ ﺭﺩ ﻪﮐ ﯽﻌﺑﺎﺗ ﻞﺧﺍﺩ ﺭﺩ ﺪﻧﺍﻮﺗ ﯽﻣ ﻂﻘﻓ ﯽﻠﺤﻣ ﺮﯿﻐﺘﻣ ﮏﯾ
ﯽﻧﺎﻬﺟ
،var
) ﻥﻼﻋﺍ ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﻥﻭﺪﺑ ﻩﺪﺷ ﺩﺎﺠﯾ
function myFunction() {
a = 4;
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>Variables created without a declaration keyword (var, let, or const) are always global,
even if they are created inside a function.:</p>
<p id="demo"></p>
<script>
myFunction();
document.getElementById("demo").innerHTML = a * a;
function myFunction() {
a = 4;
}
</script>
</body>
</html>
ﺪﯿﻨﮐ ﯽﻣ ﺶﯾﺎﻤﯿﭘ ﻪﮐ ﯽﻧﺎﻣﺯ ﺪﻨﻧﺎﻣ ،ﺪﻨﺘﺴﻫ ﻝﺎﻌﻓ ﺩﻮﺷ ﻪﺘﻓﺮﮔ ﻩﺪﯾﺩﺎﻧ ﻪﺤﻔﺻ ﻪﮐ ﯽﻧﺎﻣﺯ ﺎﺗ ﯼﺮﺳﺍﺮﺳ ﯼﺎ
ﯽﻠﺤﻣ
ﺪﯿﻫﺍﻮﺧ ﯽﻣ ﺍﺭ ﻦﯾﺍ ﻭ ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﯼﺰﯿﭼ ﺵﺭﺎﻤﺷ ﯼﺍﺮﺑ ﺮﯿﻐﺘﻣ ﮏﯾ ﺯﺍ ﺪﯿﻫﺍﻮﺧ ﯽﻣ ﺪﯿﻨﮐ ﺽﺮﻓ
:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻩﺪﻧﺭﺎﻤﺷ ﺶﯾﺍﺰﻓﺍ ﯼﺍﺮﺑ ﺩﺮﮑﻠﻤﻋ<"code class="w3-codespan> ﮏﯾ ﻭ ﯼﺮﺳﺍﺮﺳ
// Initiate counter
let counter = 0;
// Function to increment
counter
function add() {
counter += 1;
}
// Call add() 3 times
add();
add();
add();
// The counter should now be 3
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Function Closures</h2>
<p>Counting with a global variable.</p>
<p id="demo"></p>
<script>
// Initiate counter
let counter = 0;
// Function to increment counter
function add() {
counter += 1;
}
// Call add() 3 times
add();
add();
add();
// The counter should now be 3
document.getElementById("demo").innerHTML = "The counter is: " + counter;
</script>
</body>
</html>
ﻥﺁ ﻥﻭﺪﺑ ،ﺪﻫﺩ ﺮﯿﯿﻐﺗ ﺍﺭ ﻩﺪﻧﺭﺎﻤﺷ ﺪﻧﺍﻮﺗ ﯽﻣ ﻪﺤﻔﺻ ﺭﺩ ﯼﺪﮐ ﺮﻫ :ﺩﺭﺍﺩ ﺩﻮﺟﻭ ﻻﺎﺑ ﻞﺣ ﻩﺍﺭ ﺭﺩ ﯽﻠﮑﺸﻣ
.ﺩﻮﺷ ﯼﺮﯿﮔﻮﻠﺟ ﺮﮕﯾﺩ ﯼﺎﻫﺪﮐ ﺮﯿﯿﻐﺗ ﺯﺍ ﺎﺗ ﺪﺷﺎﺑ add()
ﻊﺑﺎﺗ
// Initiate counter
let counter = 0;
// Function to increment
counter
function add() {
let counter = 0;
counter += 1;
}
//
Call add() 3 times
add();
add();
add();
//The counter should
now be 3. But it is 0
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Function Closures</h2>
<p>Counting with a local variable.</p>
<p id="demo"></p>
<script>
// Initiate counter
let counter = 0;
// Function to increment counter
function add() {
let counter = 0;
counter += 1;
}
// Call add() 3 times
add();
add();
add();
// The result is not 3 because you mix up the globaland local counter
document.getElementById("demo").innerHTML = "The counter is: " + counter;
</script>
</body>
</html>
ﻢﯿﻫﺩ ﯽﻣ ﺶﯾﺎﻤﻧ ﯽﻠﺤﻣ ﯼﺎﺟ ﻪﺑ ﺍﺭ ﯽﻧﺎﻬﺟ ﻩﺪﻧﺭﺎﻤﺷ ﺎﻣ ﺍﺮﯾﺯ ﺪﻨﮐ ﯽﻤﻧ ﺭﺎﮐ ﻦﯾﺍ
ﻢﯿﻨﮐ ﺍﺪﯿﭘ ﯽﺳﺮﺘﺳﺩ ﯽﻠﺤﻣ ﻩﺪﻧﺭﺎﻤﺷ ﻪﺑ ﯽﻠﺤﻣ ﻩﺪﻧﺭﺎﻤﺷ ﻪﺑ ﻥﺩﺍﺩ ﻩﺯﺎﺟﺍ ﺎﺑ ﻭ ﻢﯿﻨﮐ ﻑﺬﺣ ﺍﺭ ﯽﻧﺎﻬﺟ ﻩﺪ
// Function to increment
counter
function add() {
let counter = 0;
counter += 1;
return counter;
}
//
Call add() 3 times
add();
add();
add();
//The counter should
now be 3. But it is 1.
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Closures</h2>
<p>Counting with a local variable.</p>
<button type="button" onclick="myFunction()">Count!</button>
<p id="demo">0</p>
<script>
// Function to increment counter
function add() {
let counter = 0;
counter += 1;
return counter;
}
// Trying to increment the counter
function myFunction(){
document.getElementById("demo").innerHTML = add();
}
</script>
</body>
</html>
ﻢﯿﻨﮐ ﯽﻣ ﯽﻧﺎﺸﻧﺯﺎﺑ ﺍﺭ ﯽﻠﺤﻣ ﻩﺪﻧﺭﺎﻤﺷ ،ﻢﯾﺮﯿﮔ ﯽﻣ ﺱﺎﻤﺗ ﻩﺭﺎﻤﺷ ﺎﺑ ﻪﮐ ﺭﺎﺑ ﺮﻫ ﺍﺮﯾﺯ ﺪﻨﮐ ﯽﻤﻧ ﺭﺎﮐ
ﺪﻨﮐ ﻞﺣ ﺍﺭ ﻞﮑﺸﻣ ﻦﯾﺍ ﺪﻧﺍﻮﺗ ﯽﻣ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﻠﺧﺍﺩ ﻊﺑﺎﺗ ﮏﯾ.
.ﺪﻧﺭﺍﺩ ﯽﺳﺮﺘﺳﺩ ﯽﻧﺎﻬﺟ ﻪﻨﻣﺍﺩ ﻪﺑ ﻊﺑﺍﻮﺗ ﻪﻤﻫ
.ﺪﻧﺭﺍﺩ ﯽﺳﺮﺘﺳﺩ «ﻻﺎﺑ» ﻩﺩﻭﺪﺤﻣ ﻪﺑ ﻊﺑﺍﻮﺗ ﻪﻤﻫ ،ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺭﺩ ،ﻊﻗﺍﻭ ﺭﺩ
ﻪﺑ ﻮﺗ ﺭﺩ ﻮﺗ ﻊﺑﺍﻮﺗ .ﺪﻨﮐ ﯽﻣ ﯽﻧﺎﺒﯿﺘﺸﭘ ﻮﺗ ﺭﺩ ﻮﺗ ﻊﺑﺍﻮﺗ ﺯﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ
ﺩﺭﺍﺩ ﯽﺳﺮﺘﺳﺩ plus()
ﯽﻠﺧﺍﺩ ﻊﺑﺎﺗ ،ﻝﺎﺜﻣ ﻦﯾﺍ ﺭﺩ
function add() {
let counter = 0;
function plus() {counter += 1;}
plus();
return counter;
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Function Closures</h2>
<p>Counting with a local variable.</p>
<p id="demo">0</p>
<script>
document.getElementById("demo").innerHTML = add();
function add() {
let counter = 0;
function plus() {counter += 1;}
plus();
return counter;
}
</script>
</body>
</html>
.ﺪﻨﮐ ﻞﺣ ﺍﺭ ﻩﺪﻧﺭﺎﻤﺷ ﻞﻀﻌﻣ ﺖﺴﻧﺍﻮﺗﯽﻣ ﻦﯾﺍ ،ﻢﯿﺳﺮﺑ .ﻢﯿﻨﮐ ﺍﺮﺟﺍ ﺭﺎﺑ ﮏﯾ ﻂﻘﻓ ﺍﺭ ﻢﯾﺭﺍﺩ ﺯﺎﯿﻧ ﯽﻠﯿﻄﻌﺗ ﻪﺑ ﺎﻣ. ؟ﺪﻫﺩ ﯽﻣ ﻡﺎﺠﻧﺍ ﯼﺭﺎﮐ ﻪﭼ ﻊﺑﺎﺗ ﻦﯾﺍ ؟ﺪﯾﺭﺍﺩ ﺮﻃﺎﺧ ﻪﺑ ﺍﺭ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺩﻮﺧ ﻊﺑﺍﻮﺗ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺩﻮﺷ ﯽﻣ ﻩﺩﺍﺩ ﺹﺎﺼﺘﺧﺍ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺩﻮﺧ ﮏﯾ ﯽﺘﺸﮔﺯﺎﺑ ﺭﺍﺪﻘﻣ ﻪﺑ (0) ﺪﻨﮐ ﯽﻣ ﺮﻔﺻ ﺍﺭ ﻩﺪﻧﺭﺎﻤﺷ .ﺩﻮﺷ ﯽﻣ ﺍﺮﺟﺍ ﺭﺎﺑ ﮏﯾ ﻂﻘﻓ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺩﻮﺧ ﻊﺑﺎﺗ ﺪﺷﺎﺑ ﻪﺘﺷﺍﺩ ﯽﺳﺮﺘﺳﺩ ﺪﻧﺍﻮﺗ ﯽﻣ ﻪﮐ ﺖﺳﺍ ﻦﯾﺍ "ﺰﯿﮕﻧﺍ ﺖﻔﮕﺷ" ﺶﺨﺑ .ﺩﻮﺷ ﯽﻣ ﻞﯾﺪﺒﺗ ﻊﺑﺎﺗ ﮏﯾ ﻪﺑ ﻥﺩﻭﺰﻓ ﺪﻨﯾﻮﮔ ﯽﻣ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻦﺘﺴﺑ ﻦﯾﺍ ﻪﺑ ،ﺩﻮﺷ ﯽﻣ ﺖﻈﻓﺎﺤﻣ ﺱﺎﻨﺷﺎﻧ ﻊﺑﺎﺗ ﻩﺩﻭﺪﺤﻣ ﻂﺳﻮﺗ ﻩﺪﻧﺭﺎﻤﺷ .ﺩﺭﺍﺩ ﯽﺳﺮﺘﺳﺩ ﺪﻟﺍﻭ ﻩﺩﻭﺪﺤﻣ ﻪﺑ ﺪﻟﺍﻭ ﻊﺑﺎﺗ ﻥﺪﺷ ﻪﺘﺴﺑ ﺯﺍ ﺲﭘ ﯽﺘﺣ ﻪﮐ ﺖﺳﺍ ﯽﻌﺑﺎﺗ ﻥﺪﺷ ﻪﺘﺴﺑplus()
counter=0
ﻪﮐ ﻢﯿﻨﮐ ﺍﺪﯿﭘ ﯽﻫ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻥﺪﺷ ﻪﺘﺴﺑ
ﻝﺎﺜﻣ
const add = (function () {
let counter = 0;
return function () {counter += 1; return counter}
})();
add();
add();
add();
// the counter is now 3
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Closures</h2>
<p>Counting with a local variable.</p>
<button type="button" onclick="myFunction()">Count!</button>
<p id="demo">0</p>
<script>
const add = (function () {
let counter = 0;
return function () {counter += 1; return counter;}
})();
function myFunction(){
document.getElementById("demo").innerHTML = add();
}
</script>
</body>
</html>
ﺪﺷ ﻩﺩﺍﺩ ﺢﯿﺿﻮﺗ ﻝﺎﺜﻣ
add