.ﺪﻨﮐ ﯽﻣ ﻦﯿﯿﻌﺗ ﺍﺭ ﺎﻫﺮﯿﻐﺘﻣ (ﻩﺪﻫﺎﺸﻣ ﺖﯿﻠﺑﺎﻗ) ﯽﺳﺮﺘﺳﺩ ﻪﻨﻣﺍﺩ
:ﺖﺳﺍ ﻪﻨﻣﺍﺩ ﻉﻮﻧ 3 ﯼﺍﺭﺍﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ
ﺪﯿﻨﮐ ﺩﻭﺪﺴﻣ ﺍﺭ ﻩﺩﻭﺪﺤﻣ
ﺩﺮﮑﻠﻤﻋ ﻩﺩﻭﺪﺤﻣ
ﯽﻧﺎﻬﺟ ﻩﺮﺘﺴﮔ
.ﺖﺷﺍﺩ Function Scope ﻭ Global Scope ﻂﻘﻓ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ،ES6 (2015) ﺯﺍ ﻞﺒﻗ
ES6 ﺩﺮﮐ ﯽﻓﺮﻌﻣ ﺍﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻢﻬﻣ ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﻭﺩ: let
.ﺪﻨﻨﮐ ﯽﻣ ﻪﺋﺍﺭﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺭﺩ ﺍﺭ Block Scope ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﻭﺩ ﻦﯾﺍ
ﺪﻨﺘﺴﯿﻧ ﯽﺳﺮﺘﺳﺩ ﻞﺑﺎﻗ { } ﮎﻮﻠﺑ ﮏﯾ ﺭﺩ ﻩﺪﺷ ﻡﻼﻋﺍ ﯼﺎﻫﺮﯿﻐﺘﻣ
{
let x = 2;
}
// x can NOT be used here
.ﺪﻨﺷﺎﺑ ﻪﺘﺷﺍﺩ ﯽﮐﻮﻠﺑ ﻩﺩﻭﺪﺤﻣ ﺪﻨﻧﺍﻮﺗ ﯽﻤﻧ var
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ
ﺖﺷﺍﺩ ﯽﺳﺮﺘﺳﺩ ﻥﺁ ﺯﺍ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ { } ﮎﻮﻠﺑ ﮏﯾ ﻞﺧﺍﺩ ﺭﺩ ﻩﺪﺷ ﻡﻼﻋﺍ ﯼﺎﻫﺮﯿﻐﺘﻣ
{
var x = 2;
}
// x CAN be used here
ﺪﻧﻮﺷ ﯽﻣ ﻞﯾﺪﺒﺗ ﻪﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺎﺗ ﮏﯾ ﺭﺩ ﻩﺪﺷ ﻡﻼﻋﺍ ﯼﺎﻫﺮﯿﻐﺘﻣ
// code here can NOT use carName
function myFunction() {
let carName = "Volvo";
// code here CAN use carName
}
// code here can NOT use carName
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Scope</h2>
<p><b>carName</b> is undefined outside myFunction():</p>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
myFunction();
function myFunction() {
let carName = "Volvo";
document.getElementById("demo1").innerHTML = typeof carName + " " + carName;
}
document.getElementById("demo2").innerHTML = typeof carName;
</script>
</body>
</html>
:ﺪﻧﺭﺍﺩ Function Scope ﯽﻠﺤﻣ ﯼﺎﻫﺮﯿﻐﺘﻣ
.ﺪﻨﺘﺴﻫ ﯽﺳﺮﺘﺳﺩ ﻞﺑﺎﻗ ﻊﺑﺎﺗ ﻞﺧﺍﺩ ﺯﺍ ﻂﻘﻓ ﺎﻬﻧﺁ
.ﺩﺮﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻒﻠﺘﺨﻣ ﻊﺑﺍﻮﺗ ﺭﺩ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﻥﺎﺴﮑﯾ ﻡﺎﻧ ﺎﺑ ﯽﯾﺎﻫﺮﯿﻐﺘﻣ ،ﺪﻧﻮﺷ ﯽﻣ ﯽﯾﺎﺳﺎﻨﺷ ﺩﻮﺧ ﻊﺑ
.ﺪﻧﻮﺷ ﯽﻣ ﻑﺬﺣ ﻊﺑﺎﺗ ﻞﯿﻤﮑﺗ ﺯﺍ ﺲﭘ ﻭ ﺪﻧﻮﺷ ﯽﻣ ﺩﺎﺠﯾﺍ ﻊﺑﺎﺗ ﮏﯾ ﻉﻭﺮﺷ ﻡﺎﮕﻨﻫ ﯽﻠﺤﻣ ﯼﺎﻫﺮﯿﻐﺘﻣ
.ﺪﻨﮐ ﯽﻣ ﺩﺎﺠﯾﺍ ﺪﯾﺪﺟ ﻩﺩﻭﺪﺤﻣ ﮏﯾ ﻊﺑﺎﺗ ﺮﻫ :ﺖﺳﺍ ﯽﻌﺑﺎﺗ ﻩﺩﻭﺪﺤﻣ ﯼﺍﺭﺍﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ
ﺪﻨﺘﺴﯿﻧ (ﻩﺪﻫﺎﺸﻣ ﻞﺑﺎﻗ) ﯽﺳﺮﺘﺳﺩ ﻞﺑﺎﻗ ﺝﺭﺎﺧ ﺯﺍ ﻊﺑﺎﺗ ﮏﯾ ﻞﺧﺍﺩ ﺭﺩ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ
var
، let
ﺎﺑ ﻩﺪﺷ ﻡﻼﻋ
:ﺪﻨﺘﺴﻫ Function Scope ﯼﺍﺭﺍﺩ ﺎﻬﻧﺁ ﻪﻤﻫ
function myFunction() {
var carName = "Volvo";
// Function Scope
}
function myFunction() {
let carName = "Volvo"; //
Function Scope
}
function myFunction() {
const carName = "Volvo"; //
Function Scope
}
.ﺩﻮﺷ ﯽﻣ GLOBAL ،ﺖﺳﺍ ﻩﺪﺷ ﻡﻼﻋﺍ ﻊﺑﺎﺗ ﮏﯾ ﺯﺍ ﺝﺭﺎﺧ ﻪﮐ ﯼﺮﯿﻐﺘﻣ
let carName = "Volvo";
// code here can use carName
function myFunction() {
// code here can also use carName
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Scope</h2>
<p>A GLOBAL variable can be accessed from any script or function.</p>
<p id="demo"></p>
<script>
let carName = "Volvo";
myFunction();
function myFunction() {
document.getElementById("demo").innerHTML = "I can display " + carName;
}
</script>
</body>
</html>
:ﺖﺳﺍ ﯽﻧﺎﻬﺟ ﻩﺮﺘﺴﮔ ﯼﺍﺭﺍﺩ ﯼﺮﺳﺍﺮﺳ ﺮﯿﻐﺘﻣ ﮏﯾ
.ﺪﻨﺷﺎﺑ ﻪﺘﺷﺍﺩ ﯽﺳﺮﺘﺳﺩ ﻥﺁ ﻪﺑ ﺪﻨﻧﺍﻮﺗ ﯽﻣ ﺏﻭ ﻪﺤﻔﺻ ﮏﯾ ﺭﺩ ﺩﻮﺟﻮﻣ ﻊﺑﺍﻮﺗ ﻭ ﺎﻫ ﺖﭙﯾﺮﮑﺳﺍ ﻪﻤﻫ
ﺪﻧﺭﺍﺩ (ﯽﻌﺑﺎﺗ ﺮﻫ ﺯﺍ ﺝﺭﺎﺧ) Globally ﻩﺪﺷ ﻡﻼﻋﺍ ﯼﺎﻫﺮﯿﻐﺘﻣ
.ﺖﺳﺍ ﯽﺳﺮﺘﺳﺩ ﻞﺑﺎﻗ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻪﻣﺎﻧﺮﺑ ﺭﺩ ﻪﻄﻘﻧ ﺮﻫ ﺯﺍ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﯽﻧﺎﻬﺟ
var
، let
ﺎﺑ ﻩﺪﺷ ﻡﻼﻋ
:ﺪﻨﺘﺴﻫ ﯽﻧﺎﻬﺟ ﻩﺮﺘﺴﮔ ﯼﺍﺭﺍﺩ ﺎﻬﻧﺁ ﻪﻤﻫ
var x = 2;
// Global scope
let x = 2; //
Global scope
const x = 2; //
Global scope
.ﺪﻨﺘﺴﻫ ﺮﯿﻐﺘﻣ ﺰﯿﻧ ﻊﺑﺍﻮﺗ ﻭ ﺎﯿﺷﺍ ،ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺭﺩ
Scope ﺪﻨﮐ ﯽﻣ ﻦﯿﯿﻌﺗ ﺍﺭ ﻊﺑﺍﻮﺗ ﻭ ءﺎﯿﺷﺍ ،ﺎﻫﺮﯿﻐﺘﻣ ﯽﺳﺮﺘﺳﺩ
.ﺩﻮﺷ ﯽﻣ ﻞﯾﺪﺒﺗ GLOBAL ﺮﯿﻐﺘﻣ ﻪﺑ ﺭﺎﮐﺩﻮﺧ ﺭﻮﻃ ﻪﺑ ،ﺪﯿﻫﺩ ﺹﺎﺼﺘﺧﺍ ﺖﺳﺍ ﻩﺪﺸﻧ ﻥﻼﻋﺍ ﻪﮐ ﯼﺮﯿﻐ
ﺮﮔﺍ ﯽﺘﺣ ،ﺪﻨﮐ ﯽﻣ ﻡﻼﻋﺍ ﺍﺭ carName
ﯽﻧﺎﻬﺟ ﺮﯿﻐﺘﻣ ﮏﯾ ﺪﮐ ﻝﺎ
myFunction();
// code here can use carName
function myFunction() {
carName = "Volvo";
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Global Variables</h2>
<p>If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable:</p>
<p id="demo"></p>
<script>
myFunction();
// code here can use carName as a global variable
document.getElementById("demo").innerHTML = "I can display " + carName;
function myFunction() {
carName = "Volvo";
}
</script>
</body>
</html>
.ﺪﻨﻨﮐ ﯽﻣ ﯽﻧﺎﺒﯿﺘﺸﭘ "ﺖﺨﺳ ﺖﻟﺎﺣ" ﺭﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯼﺍﺮﺟﺍ ﺯﺍ ﻥﺭﺪﻣ ﯼﺎﻫﺮﮔﺭﻭﺮﻣ ﻪﻤﻫ
.ﺖﻓﺮﮔ ﺪﯿﻫﺍﻮﺧ ﺩﺎﯾ ﺮﯿﮔ ﺖﺨﺳ ﺖﻟﺎﺣ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﻩﻮﺤﻧ ﺩﺭﻮﻣ ﺭﺩ ﺮﺘﺸﯿﺑ ﺵﺯﻮﻣﺁ ﻦﯾﺍ ﯼﺪﻌﺑ ﻞﺼﻓ ﺭﺩ
.ﺪﻨﺘﺴﯿﻧ ﯽﻧﺎﻬﺟ ﺭﺎﮐﺩﻮﺧ ﺭﻮﻃ ﻪﺑ ﻩﺪﺸﻧ ﻡﻼﻋﺍ ﯼﺎﻫﺮﯿﻐﺘﻣ ،"ﺖﺨﺳ ﺖﻟﺎﺣ" ﺭﺩ
.ﺖﺳﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻂﯿﺤﻣ ﯽﻧﺎﻬﺟ ﻩﺯﻮﺣ ،ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺎﺑ
.ﺖﺳﺍ ﻩﺮﺠﻨﭘ ﯽﺷ ﯽﻧﺎﻬﺟ ﻪﻨﻣﺍﺩ ،HTML ﺭﺩ
:ﺪﻧﺭﺍﺩ ﻖﻠﻌﺗ ﻩﺮﺠﻨﭘ ﯽﺷ ﻪﺑ var
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ
var carName = "Volvo";
// code here
can use window.carName
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Scope</h2>
<p>In HTML, global variables defined with <b>var</b>, will become window variables.</p>
<p id="demo"></p>
<script>
var carName = "Volvo";
// code here can use window.carName
document.getElementById("demo").innerHTML = "I can display " + window.carName;
</script>
</body>
</html>
:ﺪﻧﺭﺍﺪﻧ ﻖﻠﻌﺗ ﻩﺮﺠﻨﭘ ﯽﺷ ﻪﺑ let
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ
let carName = "Volvo";
// code here can not use window.carName
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Global Variables</h2>
<p>In HTML, global variables defined with <b>let</b>, will not become window variables.</p>
<p id="demo"></p>
<script>
let carName = "Volvo";
// code here can not use window.carName
document.getElementById("demo").innerHTML = "I can not display " + window.carName;
</script>
</body>
</html>
.ﺪﯿﺷﺎﺑ ﻪﺘﺷﺍﺩ ﺪﺼﻗ ﻪﮑﻨﯾﺍ ﺮﮕﻣ ،ﺪﯿﻨﮑﻧ ﺩﺎﺠﯾﺍ ﯼﺮﺳﺍﺮﺳ ﯼﺎﻫﺮﯿﻐﺘﻣ
ﺎﯾ) ﻩﺮﺠﻨﭘ ﯼﺎﻫﺮﯿﻐﺘﻣ ﺪﻨﻧﺍﻮﺗ ﯽﻣ ﺎﻤﺷ (ﻊﺑﺍﻮﺗ ﺎﯾ) ﯼﺮﺳﺍﺮﺳ ﯼﺎﻫﺮﯿﻐﺘﻣ
.ﺩﻮﺷ ﻡﻼﻋﺍ ﻪﮐ ﺩﻮﺷ ﯽﻣ ﻉﻭﺮﺷ ﯽﻧﺎﻣﺯ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺮﯿﻐﺘﻣ ﺮﻤﻋ ﻝﻮﻃ
.ﺪﻧﻮﺷ ﯽﻣ ﻑﺬﺣ ﻊﺑﺎﺗ ﻞﯿﻤﮑﺗ ﺯﺍ ﺲﭘ (ﯽﻠﺤﻣ) ﻊﺑﺎﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ
ﺪﻧﻮﺷ ﯽﻣ ﻑﺬﺣ ﯼﺮﺳﺍﺮﺳ ﯼﺎﻫﺮﯿﻐﺘﻣ ،ﺪﯾﺪﻨﺑ ﯽﻣ ﺍﺭ ﺮﮔﺭﻭﺮﻣ ﯽﺘﻗﻭ ،ﺏﻭ ﺮﮔﺭﻭﺮﻣ ﮏﯾ ﺭﺩ
.ﺪﻨﻨﮐ ﯽﻣ ﺭﺎﮐ ﻊﺑﺍﻮﺗ ﻞﺧﺍﺩ ﺭﺩ ﯽﻠﺤﻣ ﯼﺎﻫﺮﯿﻐﺘﻣ ﻥﺍﻮﻨﻋ ﻪﺑ (ﺎﻫﺮﺘﻣﺍﺭﺎﭘ) ﻊﺑﺎﺗ ﯼﺎﻫ ﻥﺎﻣﻮﮔﺭﺁ