ECMAScript 2009، ﻥﺍﻮﻨﻋ ﻪﺑ ﻦﯿﻨﭽﻤﻫ ES5 ﺩﻮﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﻠﺻﺍ ﻪﺨﺴﻧ ﻦﯿﻟﻭﺍ ،ﺩﻮﺷ ﯽﻣ ﻪﺘﺧﺎﻨﺷ.
.ﺪﻫﺩ ﯽﻣ ﺡﺮﺷ ﺍﺭ ES5 ﯼﺎﻫ ﯽﮔﮋﯾﻭ ﻦﯾﺮﺘﻤﻬﻣ ﻞﺼﻓ ﻦﯾﺍ
"ﻦﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺖﺨﺳ"
[ﻩﺭﺎﻤﺷ]ﻪﺘﺷﺭ ﻪﺑ ﯽﺳﺮﺘﺳﺩ
ﯽﻄﺧ ﺪﻨﭼ ﯼﺎﻫ ﻪﺘﺷﺭ
String.trim()
Array.isArray()
()ﺮﻫ ﯼﺍﺮﺑ ﻪﯾﺍﺭﺁ
() ﻪﯾﺍﺭﺁ ﻪﺸﻘﻧ
() ﻪﯾﺍﺭﺁ ﺮﺘﻠﯿﻓ
()ﻪﯾﺍﺭﺁ ﺶﻫﺎﮐ
()reduceRight ﻪﯾﺍﺭﺁ
()ﺮﻫ ﻪﯾﺍﺭﺁ
()ﺪﻨﭼ ﻪﯾﺍﺭﺁ
()indexOf ﻪﯾﺍﺭﺁ
()lastIndexOf ﻪﯾﺍﺭﺁ
JSON.parse()
JSON.stringify()
Date.now()
()toISOSstring ﺦﯾﺭﺎﺗ
()JSON ﻪﺑ ﺦﯾﺭﺎﺗ
ﺎﻫ ﻩﺪﻨﻨﮐ ﻢﯿﻈﻨﺗ ﻭ ﻥﺎﮔﺪﻧﺮﯿﮔ ﮎﻼﻣﺍ
ﻝﺍﻮﻣﺍ ﻡﺎﻧ ﻥﺍﻮﻨﻋ ﻪﺑ ﻩﺪﺷ ﻭﺭﺯﺭ ﺕﺎﻤﻠﮐ
ﯽﺷ ﯼﺎﻫ ﺵﻭﺭ
Object defineProperty()
()bind ﻊﺑﺎﺗ
ﺭﺍﺩ ﻪﻟﺎﺒﻧﺩ ﯼﺎﻫﺎﻣﺎﮐ
ES5
ﺩﻮﺷ ﯽﻣ ﯽﻧﺎﺒﯿﺘﺸﭘ ﻥﺭﺪﻣ ﯼﺎﻫﺮﮔﺭﻭﺮﻣ ﻪﻤﻫ ﺭﺩ ﻞﻣﺎﮐ ﺭﻮﻃ ﻪ
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9.0 | Yes | Yes | Yes | Yes |
"use strict"
ﺩﻮﺷ ﺍﺮﺟﺍ "ﻖﯿﻗﺩ ﺖﻟﺎﺣ" ﺭﺩ ﺪﯾﺎﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭ
.ﺪﯿﻨﮑﻧ ﻩﺩﺎﻔﺘﺳﺍ ﻩﺪﺸﻧ ﻡﻼﻋﺍ ﯼﺎﻫﺮﯿﻐﺘﻣ ﺯﺍ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺖﺨﺳ ﺖﻟﺎﺣ ﺎﺑ ،ﻝﺎﺜﻣ ﯼﺍﺮﺑ
،ﺪﯿﺴﯾﻮﻨﺑ ﯼﺮﺗ ﮎﺎﭘ ﺪﮐ ﺎﺗ ﺪﻨﮐ ﯽﻣ ﮏﻤﮐ ﺎﻤﺷ ﻪﺑ ﻦﯾﺍ .ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺩﻮﺧ ﯼﺎﻫ ﻪﻣﺎﻧﺮﺑ ﻪﻤﻫ ﺭﺩ ﺖﺨﺳ
"ﺖﺨﺳ ﻩﺩﺎﻔﺘﺳﺍ"
ﺪﻨﻨﮐ ﯽﻤﻧ ﺩﺎﺠﯾﺍ ﯽﯾﺎﻄﺧ ،ﺪﻨﻨﮑﻧ ﮎﺭﺩ ﺍﺭ ﻥﺁ
.ﺪﯿﻧﺍﻮﺨﺑ ﺮﺘﺸﯿﺑ JS Strict Mode ﺭﺩ
.ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺺﺨﺸﻣ ﺭﺍﺪﻘﻣ ﮏﯾ ﺭﺩ ﺍﺭ ﺮﺘﮐﺍﺭﺎﮐ charAt()
ﺵﻭ
var str = "HELLO WORLD";
str.charAt(0); // returns H
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript String Methods</h2>
<p>The charAt() method returns the character at a given position in a string:</p>
<p id="demo"></p>
<script>
var str = "HELLO WORLD";
document.getElementById("demo").innerHTML = str.charAt(0);
</script>
</body>
</html>
ES5 ﺪﻫﺩ ﯽﻣ ﺎﻫ ﻪﺘﺷﺭ ﺭﺩ ﺍﺭ ﯽﮔﮋﯾﻭ ﻪﺑ ﯽﺳﺮﺘﺳﺩ ﻩﺯﺎﺟﺍ:
var str = "HELLO WORLD";
str[0]; // returns H
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript String Methods</h2>
<p>ECMAScript 5 allows property acces on strings:</p>
<p id="demo"></p>
<script>
var str = "HELLO WORLD";
document.getElementById("demo").innerHTML = str[0];
</script>
</body>
</html>
.ﺪﺷﺎﺑ ﯽﻨﯿﺑ ﺶﯿﭘ ﻞﺑﺎﻗ ﺮﯿﻏ ﯽﻤﮐ ﺖﺳﺍ ﻦﮑﻤﻣ ﻪﺘﺷﺭ ﺭﺩ ﯽﮔﮋﯾﻭ ﻪﺑ ﯽﺳﺮﺘﺳﺩ
.ﺪﯿﻧﺍﻮﺨﺑ ﺮﺘﺸﯿﺑ JS String Methods ﺭﺩ
"Hello \
Dolly!";
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<p>
You can break a code line within a text string with a backslash.
</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello \
Dolly!";
</script>
</body>
</html>
ﺪﻧﻮﺷ ﻥﺎﻣﺭﺩ ﺮﺗ ﯽﻤﯾﺪﻗ ﯼﺎﻫﺮﮔﺭﻭﺮﻣ ﺖﺳﺍ ﻦﮑﻤﻣ
.ﺪﺷﺎﺑ ﻪﺘﺷﺍﺪﻧ ﯽﻧﺎﻬﺟ ﯽﻧﺎﺒﯿﺘﺸﭘ ﺖﺳﺍ ﻦﮑﻤﻣ\ﺵﻭﺭ
ﺖﺳﺍ ﻪﺘﺷﺭ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ،ﻪﻤﻠﮐ ﯽﻌﻗﺍﻭ ﯼﺎﻨﻌﻣ ﻪﺑ ﻪﺘﺷﺭ ﮏﯾ ﻦﺘﺴﮑﺷ ﯼﺍﺮﺑ ﺮﺗ ﻦﺌﻤﻄﻣ ﻩﺍﺭ ﮏﯾ
"Hello " +
"Dolly!";
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<p>The safest way to break a code line in a string is using string addition.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello " +
"Dolly!";
</script>
</body>
</html>
ES5 ﺪﻫﺩ ﯽﻣ ﻩﺯﺎﺟﺍ ﯽﯾﺍﺭﺍﺩ ﻡﺎﻧ ﻥﺍﻮﻨﻋ ﻪﺑ ﺍﺭ ﻩﺪﺷ ﻭﺭﺯﺭ ﺕﺎﻤﻠﮐ:
var obj = {name: "John", new: "yes"}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>ECMAScript 5</h1>
<p>ECMAScript 5 allows reserved words as property names.</p>
<p id="demo"></p>
<script>
var obj = {name: "John", new: "yes"};
document.getElementById("demo").innerHTML = obj.new;
</script>
</body>
</html>
trim()
ﻪﺘﺷﺭ .ﺪﻨﮐ ﯽﻣ ﻑﺬﺣ ﻪﺘﺷﺭ ﻑﺮﻃ ﻭﺩ ﺯﺍ ﺍﺭ ﯽﻟﺎﺧ ﯼﺎﻀﻓ trim()
ﺵﻭﺭ
var str = " Hello World! ";
alert(str.trim());
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The trim() Method</h2>
<p id="demo"></p>
<script>
let text1 = " Hello World! ";
let text2 = text1.trim();
document.getElementById("demo").innerHTML =
"Length text1 = " + text1.length + "<br>Length text2 = " + text2.length;
</script>
</body>
</html>
.ﺪﯿﻧﺍﻮﺨﺑ ﺮﺘﺸﯿﺑ JS String Methods ﺭﺩ
Array.isArray()
.ﺮﯿﺧ ﺎﯾ ﺖﺳﺍ ﻪﯾﺍﺭﺁ ﮏﯾ ﯽﺷ ﮏﯾ ﺎﯾﺁ ﻪﮐ ﺪﻨﮐ ﯽﻣ ﯽﺳﺭﺮﺑ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯿﻧﺍﻮﺨﺑ ﺮﺘﺸﯿﺑ JS Arrays ﺭﺩ .ﺪﻨﮐ ﯽﻣ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺭﺎﺒﮑﯾ ﻪﯾﺍﺭﺁ ﺮﺼﻨﻋ ﺮﻫ ﯼﺍﺮﺑ ﺍﺭ ﻊﺑﺎﺗ ﮏﯾ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯾﺯﻮﻣﺎﯿﺑ ﺮﺘﺸﯿﺑ JS Array Iteration Methods ﺭﺩ :ﺪﻨﮐ ﯽﻣ ﺏﺮﺿ 2 ﺭﺩ ﺍﺭ ﻪﯾﺍﺭﺁ ﺮﻫ ﺭﺍﺪﻘﻣ ﻝﺎﺜﻣ ﻦﯾﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯾﺯﻮﻣﺎﯿﺑ ﺮﺘﺸﯿﺑ JS Array Iteration Methods ﺭﺩ :ﺪﻨﮐ ﯽﻣ ﺩﺎﺠﯾﺍ 18 ﺯﺍ ﺮﺘﮔﺭﺰﺑ ﺭﺍﺪﻘﻣ ﺎﺑ ﺮﺻﺎﻨﻋ ﺯﺍ ﺪﯾﺪﺟ ﻪﯾﺍﺭﺁ ﮏﯾ ﻝﺎﺜﻣ ﻦﯾﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯾﺯﻮﻣﺎﯿﺑ ﺮﺘﺸﯿﺑ JS Array Iteration Methods ﺭﺩ :ﺪﻨﮐ ﯽﻣ ﺍﺪﯿﭘ ﺍﺭ ﻪﯾﺍﺭﺁ ﮏﯾ ﺩﺍﺪﻋﺍ ﻡﺎﻤﺗ ﻉﻮﻤﺠﻣ ﻝﺎﺜﻣ ﻦﯾﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯾﺯﻮﻣﺎﯿﺑ ﺮﺘﺸﯿﺑ JS Array Iteration Methods ﺭﺩ :ﺪﻨﮐ ﯽﻣ ﺍﺪﯿﭘ ﺍﺭ ﻪﯾﺍﺭﺁ ﮏﯾ ﺩﺍﺪﻋﺍ ﻡﺎﻤﺗ ﻉﻮﻤﺠﻣ ﻦﯿﻨﭽﻤﻫ ﻝﺎﺜﻣ ﻦﯾﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯾﺯﻮﻣﺎﯿﺑ ﺮﺘﺸﯿﺑ JS Array Iteration Methods ﺭﺩ :ﺪﻨﺘﺴﻫ 18 ﺯﺍ ﺶﯿﺑ ﺮﯾﺩﺎﻘﻣ ﻪﻤﻫ ﺎﯾﺁ ﻪﮐ ﺪﻨﮐ ﯽﻣ ﯽﺳﺭﺮﺑ ﻝﺎﺜﻣ ﻦﯾﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯾﺯﻮﻣﺎﯿﺑ ﺮﺘﺸﯿﺑ JS Array Iteration Methods ﺭﺩ :ﺪﻨﺘﺴﻫ 18 ﺯﺍ ﺶﯿﺑ ﺮﯾﺩﺎﻘﻣ ﺯﺍ ﯽﺧﺮﺑ ﺎﯾﺁ ﻪﮐ ﺪﻨﮐ ﯽﻣ ﯽﺳﺭﺮﺑ ﻝﺎﺜﻣ ﻦﯾﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯾﺯﻮﻣﺎﯿﺑ ﺮﺘﺸﯿﺑ JS Array Iteration Methods ﺭﺩ .ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺍﺭ ﻥﺁ ﺖﯿﻌﻗﻮﻣ ﻭ ﺪﯿﻨﮐ ﻮﺠﺘﺴﺟ ﺮﺼﻨﻋ ﺭﺍﺪﻘﻣ ﮏﯾ ﯼﺍﺮﺑ ﺍﺭ ﻪﯾﺍﺭﺁ ﮏﯾ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯾﺯﻮﻣﺎﯿﺑ ﺮﺘﺸﯿﺑ JS Array Iteration Methods ﺭﺩ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯾﺯﻮﻣﺎﯿﺑ ﺮﺘﺸﯿﺑ JS Array Iteration Methods ﺭﺩ .ﺖﺳﺍ ﺭﻭﺮﺳ ﺏﻭ ﮏﯾ ﺯﺍ ﺎﻫ ﻩﺩﺍﺩ ﺖﻓﺎﯾﺭﺩ JSON ﺞﯾﺍﺭ ﯼﺎﻫﺩﺮﺑﺭﺎﮐ ﺯﺍ ﯽﮑﯾ :ﺪﯾﺍ ﻩﺩﺮﮐ ﺖﻓﺎﯾﺭﺩ ﺭﻭﺮﺳ ﺏﻭ ﮏﯾ ﺯﺍ ﺍﺭ ﻦﺘﻣ ﻪﺘﺷﺭ ﻦﯾﺍ ﺪﯿﻨﮐ ﺭﻮﺼﺗ :ﺩﻮﺷ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﮏﯾ ﻪﺑ ﻦﺘﻣ ﻞﯾﺪﺒﺗ ﯼﺍﺮﺑ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯿﻧﺍﻮﺨﺑ ﺮﺘﺸﯿﺑ ﺎﻣ JSON ﺵﺯﻮﻣﺁ ﺭﺩ .ﺖﺳﺍ ﺏﻭ ﺭﻭﺮﺳ ﻪﺑ ﻩﺩﺍﺩ ﻝﺎﺳﺭﺍ JSON ﺞﯾﺍﺭ ﯼﺎﻫﺩﺮﺑﺭﺎﮐ ﺯﺍ ﯽﮑﯾ ﺪﻨﺷﺎﺑ ﺪﯾﺎﺑ ﺎﻫ ﻩﺩﺍﺩ ،ﺏﻭ ﺭﻭﺮﺳ ﻪﺑ ﻩﺩﺍﺩ ﻝﺎﺳﺭﺍ ﻡﺎﮕﻨﻫ :ﻢﯾﺭﺍﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺭﺩ ﺍﺭ ﯽﺷ ﻦﯾﺍ ﺪﯿﻨﮐ ﺭﻮﺼﺗ .ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻪﺘﺷﺭ ﻪﺑ ﻥﺁ ﻞﯾﺪﺒﺗ ﯼﺍﺮﺑ .ﺩﻮﺑ ﺪﻫﺍﻮﺧ JSON ﺩﺎﻤﻧ ﻝﺎﺒﻧﺩ ﻪﺑ ﻪﺘﺷﺭ ﮏﯾ ﻪﺠﯿﺘﻧ myJSON ﺖﺳﺍ ﺭﻭﺮﺳ ﻪﺑ ﻝﺎﺳﺭﺍ ﻩﺩﺎﻣﺁ ﻭ ﺖﺳﺍ ﻪﺘﺷﺭ ﮏﯾ ﻥﻮﻨﮐﺍ: → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯿﻧﺍﻮﺨﺑ ﺮﺘﺸﯿﺑ ﺎﻣ JSON ﺵﺯﻮﻣﺁ ﺭﺩ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯾﺯﻮﻣﺎﯿﺑ ﺮﺘﺸﯿﺑ JS Dates ﺭﺩ :ﺪﻨﮐ ﯽﻣ ﻞﯾﺪﺒﺗ ﻪﺘﺷﺭ ﻪﺑ ISO ﺩﺭﺍﺪﻧﺎﺘﺳﺍ ﺐﻟﺎﻗ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﺍﺭ Date ﯽﺷ ﮏﯾ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧisArray()
ﻝﺎﺜﻣ
function myFunction() {
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = document.getElementById("demo");
x.innerHTML = Array.isArray(fruits);
}
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The isArray() Method</h2>
<p>Click the button to check if "fruits" is an array.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = document.getElementById("demo");
x.innerHTML = Array.isArray(fruits);
}
</script>
</body>
</html>
forEach()
ﻪﯾﺍﺭﺁforEac
ﻝﺎﺜﻣ
var txt = "";
var numbers = [45, 4, 9, 16, 25];
numbers.forEach(myFunction);
function myFunction(value) {
txt = txt + value + "<br>";
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.forEach()</h2>
<p>Calls a function once for each array element.</p>
<p id="demo"></p>
<script>
var txt = "";
var numbers = [45, 4, 9, 16, 25];
numbers.forEach(myFunction);
document.getElementById("demo").innerHTML = txt;
function myFunction(value) {
txt = txt + value + "<br>";
}
</script>
</body>
</html>
map()
ﻪﯾﺍﺭﺁ ﻝﺎﺜﻣ
var numbers1 = [45, 4, 9, 16, 25];
var numbers2 = numbers1.map(myFunction);
function myFunction(value) {
return value * 2;
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.map()</h2>
<p>Creates a new array by performing a function on each array element.</p>
<p id="demo"></p>
<script>
var numbers1 = [45, 4, 9, 16, 25];
var numbers2 = numbers1.map(myFunction);
document.getElementById("demo").innerHTML = numbers2;
function myFunction(value, index, array) {
return value * 2;
}
</script>
</body>
</html>
filter()
ﻪﯾﺍﺭﺁ ﻝﺎﺜﻣ
var numbers = [45, 4, 9, 16, 25];
var over18 =
numbers.filter(myFunction);
function myFunction(value) {
return value > 18;
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.filter()</h2>
<p>Creates a new array with all array elements that passes a test.</p>
<p id="demo"></p>
<script>
var numbers = [45, 4, 9, 16, 25];
var over18 = numbers.filter(myFunction);
document.getElementById("demo").innerHTML = over18;
function myFunction(value, index, array) {
return value > 18;
}
</script>
</body>
</html>
reduce()
ﻪﯾﺍﺭﺁ ﻝﺎﺜﻣ
var numbers1 = [45, 4, 9, 16, 25];
var sum = numbers1.reduce(myFunction);
function myFunction(total, value) {
return total + value;
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.reduce()</h2>
<p>This example finds the sum of all numbers in an array:</p>
<p id="demo"></p>
<script>
var numbers = [45, 4, 9, 16, 25];
var sum = numbers.reduce(myFunction);
document.getElementById("demo").innerHTML = "The sum is " + sum;
function myFunction(total, value, index, array) {
return total + value;
}
</script>
</body>
</html>
reduceRight()
ﻪﯾﺍﺭﺁ ﻝﺎﺜﻣ
var numbers1 = [45, 4, 9, 16, 25];
var sum = numbers1.reduceRight(myFunction);
function myFunction(total, value) {
return total + value;
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.reduceRight()</h2>
<p>This example finds the sum of all numbers in an array:</p>
<p id="demo"></p>
<script>
var numbers = [45, 4, 9, 16, 25];
var sum = numbers.reduceRight(myFunction);
document.getElementById("demo").innerHTML = "The sum is " + sum;
function myFunction(total, value) {
return total + value;
}
</script>
</body>
</html>
every()
ﻪﯾﺍﺭﺁ ﻝﺎﺜﻣ
var numbers = [45, 4, 9, 16, 25];
var allOver18 =
numbers.every(myFunction);
function myFunction(value) {
return
value > 18;
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.every()</h2>
<p>The every() method checks if all array values pass a test.</p>
<p id="demo"></p>
<script>
var numbers = [45, 4, 9, 16, 25];
var allOver18 = numbers.every(myFunction);
document.getElementById("demo").innerHTML = "All over 18 is " + allOver18;
function myFunction(value, index, array) {
return value > 18;
}
</script>
</body>
</html>
some()
ﻪﯾﺍﺭﺁ ﻝﺎﺜﻣ
var numbers = [45, 4, 9, 16, 25];
var allOver18 =
numbers.some(myFunction);
function myFunction(value) {
return
value > 18;
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.some()</h2>
<p>The some() method checks if some array values pass a test.</p>
<p id="demo"></p>
<script>
var numbers = [45, 4, 9, 16, 25];
var allOver18 = numbers.some(myFunction);
document.getElementById("demo").innerHTML = "Some over 18 is " + allOver18;
function myFunction(value) {
return value > 18;
}
</script>
</body>
</html>
indexOf()
ﻪﯾﺍﺭﺁ ﻝﺎﺜﻣ
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple");
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The indexOf() Method</h2>
<p id="demo"></p>
<script>
const fruits = ["Apple", "Orange", "Apple", "Mango"];
let position = fruits.indexOf("Apple") + 1;
document.getElementById("demo").innerHTML = "Apple is found in position " + position;
</script>
</body>
</html>
lastIndexOf()
ﻪﯾﺍﺭﺁlastIndexOf()
ﻥﺎﻤﻫ indexOf
ﻝﺎﺜﻣ
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.lastIndexOf("Apple");
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The lastIndexOf() Method</h2>
<p id="demo"></p>
<script>
const fruits = ["Apple", "Orange", "Apple", "Mango"];
let position = fruits.lastIndexOf("Apple") + 1;
document.getElementById("demo").innerHTML = "Apple is found in position " + position;
</script>
</body>
</html>
JSON.parse()
'{"name":"John", "age":30, "city":"New York"}'
JSON.p
var obj = JSON.parse('{"name":"John", "age":30, "city":"New
York"}');
<!DOCTYPE html>
<html>
<body>
<h2>Creating an Object from a JSON String</h2>
<p id="demo"></p>
<script>
const txt = '{"name":"John", "age":30, "city":"New York"}'
const obj = JSON.parse(txt);
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age;
</script>
</body>
</html>
JSON.stringify()
var obj = {name:"John", age:30, city:"New York"};
JSON.stringify()
var myJSON = JSON.stringify(obj);
ﻝﺎﺜﻣ
var obj = {name:"John", age:30, city:"New York"};
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
<!DOCTYPE html>
<html>
<body>
<h2>Create a JSON string from a JavaScript object.</h2>
<p id="demo"></p>
<script>
const obj = {name: "John", age: 30, city: "New York"};
const myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
</script>
</body>
</html>
Date.now()
Date.now()
ﯽﻠﯿﻣ ﺩﺍﺪﻌﺗﻪﯿﻧﺎﺛﯽﻣﺮﺑ (ﻪﯾﻮﻧﺍﮊ ۱) ﻝﺎﺜﻣ
var timInMSs = Date.now();
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Date.now()</h2>
<p>Date.now() is new in ECMAScript 5 (2009):</p>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
document.getElementById("demo1").innerHTML = Date.now();
var d = new Date();
document.getElementById("demo2").innerHTML = d.getTime();
</script>
</body>
</html>
Date.now()
ﻪﮐ ﺍﺭ ﯼﺭﺎﮐ ﻥﺎﻤﻫ getTime() ﯽﺷ ﮏﯾ ﯼﻭﺭ
toISOSstring()
ﺦﯾﺭﺎﺗ ﻝﺎﺜﻣ
const d = new Date();
document.getElementById("demo").innerHTML = d.toISOString();
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The toISOString() Method</h2>
<p>Convert a date to a string using the ISO standard:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.toISOString();
</script>
</body>
</html>
toJSON()
ﺦﯾﺭﺎﺗtoJSON()
ﯽﺷ ﮏﯾ Date ﺦﯾﺭﺎﺗ ﻥﺍﻮﻨﻋ ﻪﺑ ﻪﮐ ﺪﻨﮐ ﯽﻣ ﻞﯾﺪﺒﺗ ﯼ
:YYYY-MM-DDTHH:mm:ss.sssZ :ﺖﺳﺍ ISO-8601 ﺩﺭﺍﺪﻧﺎﺘﺳﺍ ﺪﻨﻧﺎﻣ JSON ﯼﺎﻫ ﺦﯾﺭﺎﺗ ﺖﻣﺮﻓ
d = new Date();
document.getElementById("demo").innerHTML = d.toJSON();
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Date.toJSON()</h2>
<p id="demo"></p>
<script>
d = new Date();
document.getElementById("demo").innerHTML = d.toJSON();
</script>
</body>
</html>
ES5 ﯽﻣ ﻥﺎﮑﻣﺍ ﺎﻤﺷ ﻪﺑﺖﺳﺍ ﻢﯿﻈﻨﺗ ﺎﯾ ﺖﻓﺎﯾﺭﺩ ﻪﯿﺒﺷ ﻪﮐ ﺪﯿﻨﮐ ﻒﯾﺮﻌﺗ ﯼﻮﺤﻧ ﺎﺑ ﺍﺭ ﯽﺷ ﯼﺎﻫﺪﺘﻣ ﺎ
:ﺪﻨﮐ ﯽﻣ ﺩﺎﺠﯾﺍ fullName ﻡﺎﻧ ﻪﺑ ﯽﮔﮋﯾﻭ ﮏﯾ ﯼﺍﺮﺑ ﻩﺪﻧﺮﯿﮔ ﮏﯾ ﻝﺎﺜﻣ ﻦﯾﺍ
// Create an object:
var person = {
firstName:
"John",
lastName : "Doe",
get
fullName() {
return this.firstName + " " + this.lastName;
}
};
// Display data from the
object using a getter:
document.getElementById("demo").innerHTML =
person.fullName;
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Getters and Setters</h2>
<p>Getters and setters allow you to get and set properties via methods.</p>
<p>This example creates a getter for a property called fullName.</p>
<p id="demo"></p>
<script>
// Create an object:
var person = {
firstName: "John",
lastName : "Doe",
get fullName() {
return this.firstName + " " + this.lastName;
}
};
// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.fullName;
</script>
</body>
</html>
:ﺪﻨﮐﯽﻣ ﺩﺎﺠﯾﺍ ﻥﺎﺑﺯ ﯽﮔﮋﯾﻭ ﯼﺍﺮﺑ getter ﮏﯾ ﻭ setter
var person = {
firstName: "John",
lastName : "Doe",
language : "NO",
get lang() {
return this.language;
},
set lang(value) {
this.language = value;
}
};
// Set an object
property using a setter:
person.lang = "en";
// Display data from the
object using a getter:
document.getElementById("demo").innerHTML =
person.lang;
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Getters and Setters</h2>
<p>Getters and setters allow you to get and set properties via methods.</p>
<p>This example creates a setter and a getter for the language property.</p>
<p id="demo"></p>
<script>
// Create an object:
var person = {
firstName: "John",
lastName : "Doe",
language : "NO",
get lang() {
return this.language;
},
set lang(value) {
this.language = value;
}
};
// Set an object property using a setter:
person.lang = "en";
// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.lang;
</script>
</body>
</html>
:ﺪﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﻥﺎﺑﺯ ﮒﺭﺰﺑ ﻑﻭﺮﺣ ﯼﺎﻫ ﯽﻧﺎﺳﺭ ﺯﻭﺭ ﻪﺑ ﻥﺩﺮﮐ ﻦﻤﯾﺍ ﯼﺍﺮﺑ ﻩﺪﻨﻨﮐ ﻢﯿﻈﻨﺗ ﮏﯾ ﺯﺍ ﻝﺎﺜﻣ
var person = {
firstName: "John",
lastName : "Doe",
language : "NO",
set lang(value) {
this.language = value.toUpperCase();
}
};
// Set an object
property using a setter:
person.lang = "en";
// Display data from the
object:
document.getElementById("demo").innerHTML =
person.language;
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Getters and Setters</h2>
<p>Getters and setters allow you to get and set properties via methods.</p>
<p>This example has a modified setter to secure upper case uppdates of language.</p>
<p id="demo"></p>
<script>
// Create an object:
var person = {
firstName: "John",
lastName : "Doe",
language : "",
set lang(value) {
this.language = value.toUpperCase();
}
};
// Set an object property using a setter:
person.lang = "en";
// Display data from the object:
document.getElementById("demo").innerHTML = person.language;
</script>
</body>
</html>
ﺪﯾﺯﻮﻣﺎﯿﺑ ﺮﺘﺸﯿﺑ JS Object Accessors ﺭﺩ ﺎﻫ Setter ﻭ Gettes ﻩﺭﺎﺑﺭﺩ
Object.defineProperty()
Object.defineProperty()
ﺵﻭﺭ ﮏﯾ Object ﺭﺩ ﺪﯾﺪﺟ ES5 ﺖﺳ
ﺪﯿﻫﺩ ﺮﯿﯿﻐﺗ ﺍﺭ ﯽﮔﮋﯾﻭ ﮏﯾ ﺎﯾ/ﻭ ﺭﺍﺪﻘﻣ ﺎﯾ/ﻭ ﺪﯿﻨﮐ ﻒﯾﺮﻌﺗ ﺍﺭ ﯽﺷ ﯽﮔﮋﯾﻭ ﮏﯾ ﺪﻫﺩ ﯽﻣ ﻥﺎﮑﻣﺍ ﺎﻤﺷ ﻪﺑ
// Create an Object:
var person = {
firstName:
"John",
lastName : "Doe",
language : "NO",
};
// Change a Property:
Object.defineProperty(person, "language", {
value: "EN",
writable : true,
enumerable : true,
configurable : true
});
//
Enumerate Properties
var txt = "";
for (var x in person) {
txt += person[x] + "<br>";
}
document.getElementById("demo").innerHTML =
txt;
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=iso-8859-2" http-equiv="Content-Type">
</head>
<body>
<h2>JavaScript defineProperty()</h2>
<p id="demo"></p>
<script>
// Create an Object:
var person = {
firstName: "John",
lastName : "Doe",
language : "NO",
};
// Change a Property:
Object.defineProperty(person, "language", {
value: "EN",
writable : true,
enumerable : true,
configurable : true
});
// Enumerate Properties
txt = "";
for (var x in person) {
txt += person[x] + "<br>";
}
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
:ﺪﻨﮐ ﯽﻣ ﻥﺎﻬﻨﭘ enumeration ﺯﺍ ﺍﺭ ﻥﺎﺑﺯ ﯽﮔﮋﯾﻭ ﻪﮐ ﺕﻭﺎﻔﺗ ﻦﯾﺍ ﺎﺑ ،ﺖﺳﺍ ﺪﮐ ﻥﺎﻤﻫ ﯼﺪﻌﺑ ﻝﺎﺜﻣ
// Create an Object:
var person = {
firstName:
"John",
lastName : "Doe",
language : "NO",
};
// Change a Property:
Object.defineProperty(person, "language", {
value: "EN",
writable : true,
enumerable : false,
configurable : true
});
//
Enumerate Properties
var txt = "";
for (var x in person) {
txt += person[x] + "<br>";
}
document.getElementById("demo").innerHTML =
txt;
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript defineProperty()</h2>
<p id="demo"></p>
<script>
// Create an Object:
var person = {
firstName: "John",
lastName : "Doe",
language : "NO",
};
// Change a Property:
Object.defineProperty(person, "language", {
value: "EN",
writable : true,
enumerable : false,
configurable : true
});
// Enumerate Properties
txt = "";
for (var x in person) {
txt += person[x] + "<br>";
}
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
:ﺪﻨﮐ ﯽﻣ ﺩﺎﺠﯾﺍ ﻥﺎﺑﺯ ﮒﺭﺰﺑ ﻑﻭﺮﺣ ﯼﺎﻫ ﯽﻧﺎﺳﺭ ﺯﻭﺭ ﻪﺑ ﻥﺩﺮﮐ ﻦﻤﯾﺍ ﯼﺍﺮﺑ ﻩﺪﻧﺮﯿﮔ ﮏﯾ ﻭ ﻩﺪﻨﻨﮐ ﻢﯿﻈﻨﺗ
// Create an Object:
var person = {
firstName: "John",
lastName :
"Doe",
language : "NO"
};
// Change a Property:
Object.defineProperty(person, "language", {
get : function() { return
language },
set : function(value) { language = value.toUpperCase()}
});
// Change Language
person.language = "en";
// Display Language
document.getElementById("demo").innerHTML = person.language;
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript defineProperty()</h2>
<p id="demo"></p>
<script>
// Create an Object:
var person = {
firstName: "John",
lastName : "Doe",
language : "NO"
};
// Change a Property:
Object.defineProperty(person, "language", {
get : function() { return language },
set : function(value) { language = value.toUpperCase()}
});
// Change language
person.language = "en";
// Display language
document.getElementById("demo").innerHTML = person.language;
</script>
</body>
</html>
ES5 ﺵﻭﺭ ﺯﺍ ﯼﺭﺎﯿﺴﺑﺩﺮﮐ ﻪﻓﺎﺿﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻪﺑ ﺍﺭ ﺪﯾﺪﺟ ﯽﺷ ﯼﺎﻫ:
// Create object with an existing object as prototype
Object.create(parent, donor)
// Adding or changing an object property
Object.defineProperty(object, property, descriptor)
// Adding or changing object properties
Object.defineProperties(object, descriptors)
// Accessing Properties
Object.getOwnPropertyDescriptor(object, property)
// Returns all properties as an array
Object.getOwnPropertyNames(object)
// Accessing the prototype
Object.getPrototypeOf(object)
// Returns enumerable properties as an array
Object.keys(object)
// Prevents adding properties to an object
Object.preventExtensions(object)
// Returns true if properties can be added to an object
Object.isExtensible(object)
// Prevents changes of object properties (not values)
Object.seal(object)
// Returns true if object is sealed
Object.isSealed(object)
// Prevents any changes to an object
Object.freeze(object)
// Returns true if object is frozen
Object.isFrozen(object)
.ﺪﯾﺯﻮﻣﺎﯿﺑ ﺮﺘﺸﯿﺑ Object ECMAScript5 ﺭﺩ
Bind()
ﻊﺑﺎﺗ.ﺩﺮﯿﮕﺑ ﺽﺮﻗ ﺮﮕﯾﺩ ﯽﺷ ﮏﯾ ﺯﺍ ﺍﺭ ﺪﺘﻣ ﮏﯾ ﺪﻧﺍﻮﺗ ﯽﻣ ﯽﺷ ﮏﯾ ،bind()
.ﺪﻨﮐ ﯽﻣ ﺩﺎﺠﯾﺍ (ﻮﻀﻋ ﻭ ﺺﺨﺷ) ﯽﺷ 2 ﻝﺎﺜﻣ ﻦﯾﺍ
:ﺩﺮﯿﮔ ﯽﻣ ﺽﺮﻗ ﺺﺨﺷ ﯽﺷ ﺯﺍ ﺍﺭ fullname ﺪﺘﻣ ﻮﻀﻋ ﯽﺷ
const person = {
firstName:"John",
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
}
}
const member = {
firstName:"Hege",
lastName: "Nilsen",
}
let fullName = person.fullName.bind(member);
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Function bind()</h1>
<p>This example creates 2 objects (person and member).</p>
<p>The member object borrows the fullname method from person:</p>
<p id="demo"></p>
<script>
const person = {
firstName:"John",
lastName: "Doe",
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
const member = {
firstName:"Hege",
lastName: "Nilsen",
}
let fullName = person.fullName.bind(member);
document.getElementById("demo").innerHTML = fullName();
</script>
</body>
</html>
.ﺪﯾﺯﻮﻣﺎﯿﺑ ﺮﺘﺸﯿﺑ()Function bind ﺭﺩ
ES5 ﺪﺷﺎﺑ ﻪﺘﺷﺍﺩ ﺩﻮﺟﻭ ﻪﯾﺍﺭﺁ ﻭ ﯽﺷ ﻒﯾﺭﺎﻌﺗ ﺭﺩ ﺭﺍﺩ ﻪﻟﺎﺒﻧﺩ ﯼﺎﻫﺎﻣﺎﮐ ﺎﺗ ﺪﻫﺩ ﯽﻣ ﻩﺯﺎﺟﺍ:
person = {
firstName: "John",
lastName: "
Doe",
age: 46,
}
points = [
1,
5,
10,
25,
40,
100,
];
!!!ﺭﺍﺪﺸﻫ
JSON ﺪﻫﺩ ﯽﻤﻧ ﺍﺭ ﯽﯾﺎﻬﺘﻧﺍ ﯼﺎﻫﺎﻣﺎﮐ ﻩﺯﺎﺟﺍ.
//
Allowed:
var person = '{"firstName":"John", "lastName":"Doe",
"age":46}'
JSON.parse(person)
// Not allowed:
var person = '{"firstName":"John",
"lastName":"Doe", "age":46,}'
JSON.parse(person)
//
Allowed:
points = [40, 100, 1, 5, 25, 10]
// Not allowed:
points =
[40, 100, 1, 5, 25, 10,]