ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻪﻨﯾﺰﻫ


فهرست مطالب

    نمایش فهرست مطالب

ﺪﺷ ﯽﻓﺮﻌﻣ const ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ

ﺩﺮﮐ ﻡﻼﻋﺍ ﻩﺭﺎﺑﻭﺩ ﻥﺍﻮﺗﯽﻤﻧ ﺍﺭ const ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ

ﺪﻨﺘﺴﯿﻧ ﺩﺪﺠﻣ ﺺﯿﺼﺨﺗ ﻞﺑﺎﻗ const ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ

ﺪﻨﺘﺴﻫ Block Scope ﯼﺍﺭﺍﺩ const ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ

ﺖﺴﯿﻧ ﺩﺪﺠﻣ ﯼﺭﺍﺬﮔﺍﻭ ﻞﺑﺎﻗ

:ﺩﺍﺩ ﺹﺎﺼﺘﺧﺍ ﻩﺭﺎﺑﻭﺩ ﻥﺍﻮﺗ ﯽﻤﻧ ﺍﺭ const ﺮﯿﻐﺘﻣ ﮏﯾ

ﻝﺎﺜﻣ

const PI = 3.141592653589793;
PI = 3.14;      // This will give an error
PI = PI + 10;   // This will also give an error

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript const</h2>

<p id="demo"></p>

<script>
try {
  const PI = 3.141592653589793;
  PI = 3.14;
}
catch (err) {
  document.getElementById("demo").innerHTML = err;
}
</script>

</body>
</html>

ﺩﻮﺷ ﻩﺩﺍﺩ ﺹﺎﺼﺘﺧﺍ ﺪﯾﺎﺑ

:ﺩﻮﺷ ﻩﺩﺍﺩ ﺹﺎﺼﺘﺧﺍ ﺎﻬﻧﺁ ﻪﺑ ﺭﺍﺪﻘﻣ ﮏﯾ ﺪﻧﻮﺷﯽﻣ ﻥﻼﻋﺍ ﻪﮐ ﯽﻧﺎﻣﺯ ﺪﯾﺎﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ const

ﺖﺳﺭﺩ

const PI = 3.14159265359;

ﻂﻠﻏ

const PI;
PI = 3.14159265359;

؟ﻢﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ JavaScript const ﺯﺍ ﯽﻧﺎﻣﺯ ﻪﭼ

ﺎﺑ ﺍﺭ ﺮﯿﻐﺘﻣ ﮏﯾ ﻪﺸﯿﻤﻫ const ﻪﮐ ﺪﯿﻧﺍﺩ ﯽﻣ ﯽﺘﻗﻭ ﺪﯿﻨﮐ ﻡﻼﻋﺍ

:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ const ﺯﺍ ﺪﯿﻨﮐ ﯽﻣ ﻡﻼﻋﺍ ﯽﺘﻗﻭ

  • ﺪﯾﺪﺟ ﻪﯾﺍﺭﺁ ﮏﯾ

  • ﺪﯾﺪﺟ ﯽﺷ ﮏﯾ

  • ﺪﯾﺪﺟ ﺩﺮﮑﻠﻤﻋ ﮏﯾ

  • ﺪﯾﺪﺟ RegExp ﮏﯾ


ﺖﺑﺎﺛ ﯼﺎﻫ ﻪﯾﺍﺭﺁ ﻭ ﺎﯿﺷﺍ

.ﺖﺳﺍ ﻩﺪﻨﻨﮐ ﻩﺍﺮﻤﮔ ﯽﻤﮐ const ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ

.ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ ﺍﺭ ﺭﺍﺪﻘﻣ ﮏﯾ ﻪﺑ ﺖﺑﺎﺛ ﻉﺎﺟﺭﺍ ﮏﯾ .ﺪﻨﮐ ﯽﻤﻧ ﻒﯾﺮﻌﺗ ﺍﺭ ﯽﺘﺑﺎﺛ ﺭﺍﺪﻘﻣ

:ﺪﯿﻧﺍﻮﺗ ﯽﻤﻧ ﺎﻤﺷ ﻞﯿﻟﺩ ﻦﯿﻤﻫ ﻪﺑ

  • ﺪﯿﻫﺩ ﺹﺎﺼﺘﺧﺍ ﻩﺭﺎﺑﻭﺩ ﺍﺭ ﺖﺑﺎﺛ ﺭﺍﺪﻘﻣ ﮏﯾ

  • ﺪﯿﻫﺩ ﺹﺎﺼﺘﺧﺍ ﻩﺭﺎﺑﻭﺩ ﺍﺭ ﺖﺑﺎﺛ ﻪﯾﺍﺭﺁ ﮏﯾ

  • ﺪﯿﻫﺩ ﺹﺎﺼﺘﺧﺍ ﻩﺭﺎﺑﻭﺩ ﺍﺭ ﺖﺑﺎﺛ ﯽﺷ ﮏﯾ

:ﯽﻧﺍﻮﺗ ﯽﻣ ﻮﺗ ﯽﻟﻭ

  • ﺪﯿﻫﺩ ﺮﯿﯿﻐﺗ ﺍﺭ ﺖﺑﺎﺛ ﻪﯾﺍﺭﺁ ﺮﺻﺎﻨﻋ

  • ﺪﯿﻫﺩ ﺮﯿﯿﻐﺗ ﺍﺭ ﺖﺑﺎﺛ ﻢﺴﺟ ﯼﺎﻫ ﯽﮔﮋﯾﻭ


ﺖﺑﺎﺛ ﯼﺎﻫ ﻪﯾﺍﺭﺁ

:ﺪﯿﻫﺩ ﺮﯿﯿﻐﺗ ﺍﺭ ﺖﺑﺎﺛ ﻪﯾﺍﺭﺁ ﮏﯾ ﺮﺻﺎﻨﻋ ﺪﯿﻧﺍﻮﺗ ﯽﻣ

ﻝﺎﺜﻣ

// You can create a constant array:
const cars = ["Saab", "Volvo", "BMW"];

// You can change an element:
cars[0] = "Toyota";

// You can add an element:
cars.push("Audi");

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript const</h2>

<p>Declaring a constant array does NOT make the elements unchangeable:</p>

<p id="demo"></p>

<script>
// Create an Array:
const cars = ["Saab", "Volvo", "BMW"];

// Change an element:
cars[0] = "Toyota";

// Add an element:
cars.push("Audi");

// Display the Array:
document.getElementById("demo").innerHTML = cars; 
</script>

</body>
</html>

:ﺪﯿﻫﺩ ﺹﺎﺼﺘﺧﺍ ﻩﺭﺎﺑﻭﺩ ﺍﺭ ﻪﯾﺍﺭﺁ ﺪﯿﻧﺍﻮﺗ ﯽﻤﻧ ﺎﻤﺷ ﺎﻣﺍ

ﻝﺎﺜﻣ

const cars = ["Saab", "Volvo", "BMW"];

cars = ["Toyota", "Volvo", "Audi"];    // ERROR

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript const</h2>

<p>You can NOT reassign a constant array:</p>

<p id="demo"></p>

<script>
try {
  const cars = ["Saab", "Volvo", "BMW"];
  cars = ["Toyota", "Volvo", "Audi"];
}
catch (err) {
  document.getElementById("demo").innerHTML = err;
}
</script>

</body>
</html>

ﺖﺑﺎﺛ ءﺎﯿﺷﺍ

:ﺪﯿﻫﺩ ﺮﯿﯿﻐﺗ ﺍﺭ ﺖﺑﺎﺛ ﯽﺷ ﮏﯾ ﯼﺎﻫ ﯽﮔﮋﯾﻭ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺎﻤﺷ

ﻝﺎﺜﻣ

// You can create a const object:
const car = {type:"Fiat", model:"500", color:"white"};

// You can change a property:
car.color = "red";

// You can add a property:
car.owner = "Johnson";

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript const</h2>

<p>Declaring a constant object does NOT make the objects properties unchangeable:</p>

<p id="demo"></p>

<script>
// Create an object:
const car = {type:"Fiat", model:"500", color:"white"};

// Change a property:
car.color = "red";

// Add a property:
car.owner = "Johnson";

// Display the property:
document.getElementById("demo").innerHTML = "Car owner is " + car.owner; 
</script>

</body>
</html>

:ﺪﯿﻫﺩ ﺹﺎﺼﺘﺧﺍ ﻩﺭﺎﺑﻭﺩ ﺍﺭ ﯽﺷ ﺪﯿﻧﺍﻮﺗ ﯽﻤﻧ ﺎﻤﺷ ﺎﻣﺍ

ﻝﺎﺜﻣ

const car = {type:"Fiat", model:"500", color:"white"};

car = {type:"Volvo", model:"EX60", color:"red"};    // 
  ERROR

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript const</h2>

<p>You can NOT reassign a constant object:</p>

<p id="demo"></p>

<script>
try {
  const car = {type:"Fiat", model:"500", color:"white"};
  car = {type:"Volvo", model:"EX60", color:"red"};
}
catch (err) {
  document.getElementById("demo").innerHTML = err;
}
</script>

</body>
</html>

const ﻭ var، let ﻦﯿﺑ ﺕﻭﺎﻔﺗ

ScopeRedeclareReassignHoistedBinds this
varNoYesYesYesYes
letYesNoYesNoNo
constYesNoNoNoNo

؟ﻪﺑﻮﺧ ﯽﭼ

ﺪﯿﻫﺩ ﻩﺯﺎﺟﺍconst

ﺪﯿﻫﺩ ﻩﺯﺎﺟﺍconst

ﺪﯿﻫﺩ ﻩﺯﺎﺟﺍconst

ﺪﯿﻫﺩ ﻩﺯﺎﺟﺍconst

ﺪﯿﻫﺩ ﻩﺯﺎﺟﺍconst

؟ﺖﺴﯿﻧ ﺏﻮﺧ ﯼﺰﯿﭼ ﻪﭼ

var ﺩﻮﺷ ﻡﻼﻋﺍ ﺖﺴﯿﻧ ﻡﺯﻻ.

var ﺖﺳﺍ ﻪﺘﻓﺎﯾ ﺶﯾﺍﺰﻓﺍ.

var ﺖﺳﺍ ﻞﺼﺘﻣ ﻦﯾﺍ ﻪﺑ.


ﺮﮔﺭﻭﺮﻣ ﯽﻧﺎﺒﯿﺘﺸﭘ

ﺪﻨﺘﺴﻫ constlet ﯼﺪﯿﻠﮐ ﺕﺎﻤﻠﮐ

:ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ ﺍﺭ ﻞﻣﺎﮐ ﯽﻧﺎﺒﯿﺘﺸﭘ ﺎﺑ ﺮﮔﺭﻭﺮﻣ ﯼﺎﻫ ﻪﺨﺴﻧ ﻦﯿﻟﻭﺍ ﺮﯾﺯ ﻝﻭﺪﺟ

Chrome 49 Edge 12 Firefox 36 Safari 11 Opera 36
Mar, 2016 Jul, 2015 Jan, 2015 Sep, 2017 Mar, 2016


Block Scope

.ﺖﺳﺍ let ﻪﺑﺎﺸﻣ const ﺎﺑ ﺮﯿﻐﺘﻣ ﮏﯾ ﻥﻼﻋﺍ

x ﺎﺑ ،ﻝﺎﺜﻣ ﻦﯾﺍ ﺭﺩ ،ﮎﻮﻠﺑ ﺭﺩ ﻩﺪﺷ ﻡﻼﻋﺍ x ﺖﺴﯿﻧ ﻥﺎﺴﮑﯾ ﮎﻮﻠﺑ ﺯﺍ ﺝﺭﺎﺧ ﺭﺩ ﻩﺪﺷ ﻡﻼﻋﺍ:

ﻝﺎﺜﻣ

const x = 10;
// Here x is 10

{ 
const x = 2;
// Here x is 2
}

// Here x is 10

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScropt const variables has block scope</h2>

<p id="demo"></p>

<script>
const  x = 10;
// Here x is 10

{  
const x = 2;
// Here x is 2
}

// Here x is 10
document.getElementById("demo").innerHTML = "x is " + x;
</script>

</body>
</html>


.ﺪﯿﻨﮐ ﺐﺴﮐ ﯼﺮﺘﺸﯿﺑ ﺕﺎﻋﻼﻃﺍ ﮎﻮﻠﺑ ﻩﺩﻭﺪﺤﻣ ﻩﺭﺎﺑﺭﺩ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺶﺨﺑ ﺭﺩ


ﺩﺪﺠﻣ ﻡﻼﻋﺍ

ﺖﺳﺍ ﺯﺎﺠﻣ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ var ﺮﯿﻐﺘﻣ ﺩﺪﺠﻣ ﻡﻼﻋﺍ

ﻝﺎﺜﻣ

var x = 2;     // Allowed
var x = 3;     // Allowed
  x = 4;         // Allowed

ﺩﻮﺟﻮﻣ ﻩﺯﺎﺟﺍ<"code class="w3-codespan> ﺎﯾ var ﺩﺪﺠﻣ ﻡﻼﻋﺍ

ﻝﺎﺜﻣ

var x = 2;     // Allowed
const x = 2;   // Not allowed

{
let x = 2;     // Allowed
const x = 2;   // Not allowed
}

{
const x = 2;   // Allowed
const x = 2;   // Not allowed
}
  

:ﺖﺴﯿﻧ ﺯﺎﺠﻣ ،ﻩﺩﻭﺪﺤﻣ ﻥﺎﻤﻫ ﺭﺩ ،ﺩﻮﺟﻮﻣ const ﺮﯿﻐﺘﻣ ﮏﯾ ﺩﺪﺠﻣ ﺺﯿﺼﺨﺗ

ﻝﺎﺜﻣ

 const x = 2;     // Allowed
  x = 2;           // Not allowed
  var x = 2;       // Not allowed
  let x = 2;       // Not allowed
  const x = 2;     // Not allowed
  
{	  const x = 2;   // Allowed
  x = 2;         
  // Not allowed
  var x = 2;     
  // Not allowed
  let x = 2;     
  // Not allowed
   
  const x = 2;   // Not allowed
}
  

:ﺖﺳﺍ ﺯﺎﺠﻣ ،ﺮﮕﯾﺩ ﮎﻮﻠﺑ ﺭﺩ ﺎﯾ ،ﺮﮕﯾﺩ ﻩﺩﻭﺪﺤﻣ ﺭﺩ ،const ﺎﺑ ﺮﯿﻐﺘﻣ ﮏﯾ ﺩﺪﺠﻣ ﻡﻼﻋﺍ

ﻝﺎﺜﻣ

 const x = 2;       // Allowed
{	  const x = 3;   // Allowed
  }
  
  {
  const x = 4;   // Allowed
  }

ﻥﺩﺮﺑ ﻻﺎﺑ

ﺪﻨﺘﺴﻫ ﻻﺎﺑ ﻻﺎﺑ ﺭﺩ var ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ

:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻥﺁ ﺯﺍ ﺩﻮﺷ ﻡﻼﻋﺍ ﺮﯿﻐﺘﻣ ﻪﮑﻨﯾﺍ ﺯﺍ ﻞﺒﻗ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺎﻤﺷ :ﯽﻨﻌﻣ

ﻝﺎﺜﻣ

:ﺖﺳﺍ ﺏﻮﺧ ﻦﯾﺍ

 carName = "Volvo";
  var carName;

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Hoisting</h2>

<p>With <b>var</b>, you can use a variable before it is declared:</p>

<p id="demo"></p>

<script>
carName = "Volvo";
document.getElementById("demo").innerHTML = carName;
var carName;
</script>

</body>
</html>

.ﺪﯿﻨﮐ ﻪﻌﻟﺎﻄﻣ ﺍﺭ Hoisting ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻞﺼﻓ ،ﺪﯿﻧﺍﺪﺑ ﺮﺘﺸﯿﺑ ﻥﺩﺮﺑ ﻻﺎﺑ ﺩﺭﻮﻣ ﺭﺩ ﺪﯿﻫﺍﻮﺧ ﯽﻣ ﺮﮔﺍ

ﮏﯾ ﻪﺑ ﺮﺠﻨﻣ ﻥﻼﻋﺍ ﺯﺍ ﻞﺒﻗ const ﺮﯿﻐﺘﻣ ﮏﯾ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ :ﯽﻨﻌﻣ

ﻝﺎﺜﻣ

alert (carName);
const carName = "Volvo";

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Hoisting</h2>
<p>With <b>const</b>, you cannot use a variable before it is declared:</p>
<p id="demo"></p>

<script>

try {
  alert(carName);
  const carName = "Volvo";
}
catch (err) {
  document.getElementById("demo").innerHTML = err;
}

</script>
</body>
</html>

تمامی حقوق محفوظ است. © BasicIT.org • 2023-2024