.ﺩﺮﮐ ﺭﺍﺮﮑﺗ for..of
ﺎﺑ ﺍﺭ ﺎﻬﻧﺁ ﻥﺍﻮﺗﯽﻣ ﻪﮐ ﺪﻨﺘﺴﻫ ﯽ
.ﺪﻨﻨﮐ ﯼﺯﺎﺳ ﻩﺩﺎﯿﭘ ﺍﺭ Symbol.iterator
ﺵﻭﺭ ﺪﯾﺎﺑ ﺎﻫﺮﯾﺬﭘﺭ
:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻪﺘﺷﺭ ﮏﯾ ﺮﺻﺎﻨﻋ ﯼﻭﺭ ﺭﺍﺮﮑﺗ ﯼﺍﺮﺑ for..of
ﻪ
for (const x of "W3Schools") {
// code block to be executed
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Iterables</h2>
<p>Iterate over a String:</p>
<p id="demo"></p>
<script>
// Create a String
const name = "W3Schools";
// List all Elements
let text = ""
for (const x of name) {
text += x + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻪﯾﺍﺭﺁ ﮏﯾ ﺮﺻﺎﻨﻋ ﯼﻭﺭ ﺭﺍﺮﮑﺗ ﯼﺍﺮﺑ for..of
for (const x of [1,2,3,4,5]) {
// code block to be executed
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Iterables</h2>
<p>Iterate over an Array:</p>
<p id="demo"></p>
<script>
// Create aa Array
const letters = ["a","b","c"];
// List all Elements
let text = "";
for (const x of letters) {
text += x + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
ﻩﺪﻨﻨﮐ ﺭﺍﺮﮑﺗ ﻞﮑﺗﻭﺮﭘ ﺪﯿﻟﻮﺗ ﻩﻮﺤﻧ ﺮﯾﺩﺎﻘﻣ ﯽﻟﺍﻮﺗ ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ ﯽﺷ ﮏﯾ ﺯﺍ ﺍﺭ.
.ﺩﻮﺷ ﯽﻣ ﻞﯾﺪﺒﺗ iterator ﻪﺑ ﺪﻨﮐ ﯽﻣ ﯼﺯﺎﺳ ﻩﺩﺎﯿﭘ ﺍﺭ next(
:ﺪﻧﺍﺩﺮﮔﺮﺑ ﯽﮔﮋﯾﻭ ﻭﺩ ﺎﺑ ﯽﺷ ﮏﯾ ﺪﯾﺎﺑ next()
ﺪﺘﻣ
(ﯼﺪﻌﺑ ﺭﺍﺪﻘﻣ) ﺵﺯﺭﺍ
(ﺖﺳﺭﺩﺎﻧ ﺎﯾ ﺖﺳﺭﺩ) ﻩﺪﺷ ﻡﺎﺠﻧﺍ
ﺖﺳﺭﺩ ﺪﺷﺎﺑ ﻩﺪﺷ ﻞﻣﺎﮐ ﻩﺪﻨﻨﮐ ﺭﺍﺮﮑﺗ ﺮﮔﺍ
<"code class="w3-codespan> ﻥﺎﻣﺯ ﺮﻫ ....،10،20،30،40 :ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﻥﺎﯾﺎﭘ ﯽﺑ ﯼﺭﺍﺮﮑﺗ ﻦﯾﺍ
// Home Made Iterable
function myNumbers() {
let n = 0;
return {
next: function() {
n += 10;
return {value:n, done:false};
}
};
}
// Create Iterable
const n = myNumbers();
n.next(); // Returns 10
n.next(); // Returns 20
n.next(); // Returns 30
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Iterables</h2>
<p>Home Made Iterable:</p>
<p id="demo"></p>
<script>
// Home Made Iterable
function myNumbers() {
let n = 0;
return {
next: function() {
n += 10;
return {value:n, done:false};
}
};
}
// Create Iterable
const n = myNumbers();
n.next(); // 10
n.next(); // 20
n.next(); // 30
document.getElementById("demo").innerHTML = n.next().value;
</script>
</body>
</html>
:ﺖﺳﺍ ﺭﺍﺮﮑﺗ ﻞﺑﺎﻗ ﻪﮐ ﯼﺍ ﻪﻧﺎﺧ ﻞﮑﺸﻣ
.ﺪﻨﮐ ﯽﻤﻧ ﯽﻧﺎﺒﯿﺘﺸﭘ ﺍﺭ for..of
ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺭﻮﺘﺳﺩ ﻦﯾﺍ
.ﺖﺳﺍ Symbol.iterator ﯼﺍﺭﺍﺩ ﻪﮐ ﺖﺳﺍ ﯽﺌﯿﺷ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺮﯾﺬﭘﺭﺍﺮﮑﺗ
Symbol.iterator
ﻊﺑﺎﺗ ﻪﮐ ﺖﺳﺍ ﯽﻌﺑﺎﺗ for> :ﺩﺮﮐ ﺭﺍﺮﮑﺗ ﺪﮐ ﻦﯾﺍ ﺎﺑ ﻥ
// Create an Object
myNumbers = {};
// Make it Iterable
myNumbers[Symbol.iterator] = function() {
let n = 0;
done = false;
return {
next() {
n += 10;
if (n == 100) {done = true}
return {value:n, done:done};
}
};
}
ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Iterables</h2>
<p id="demo"></p>
<script>
// Create an Object
myNumbers = {};
// Make it Iterable
myNumbers[Symbol.iterator] = function() {
let n = 0;
done = false;
return {
next() {
n += 10;
if (n == 100) {done = true}
return {value:n, done:done};
}
};
}
let text = ""
for (const num of myNumbers) {
text += num +"<br>"
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
.ﺩﻮﺷ ﯽﻣ ﯽﻧﺍﻮﺧﺍﺮﻓ for..of
ﻂﺳﻮﺗ ﺭﺎﮐﺩﻮﺧ ﺭﻮﻃ ﻪﺑ Symbol.i
let iterator = myNumbers[Symbol.iterator]();
while (true) {
const result = iterator.next();
if (result.done) break;
// Any Code Here
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Iterables</h2>
<p id="demo"></p>
<script>
// Create an Object
myNumbers = {};
// Make it Iterable
myNumbers[Symbol.iterator] = function() {
let n = 0;
done = false;
return {
next() {
n += 10;
if (n == 100) {done = true}
return {value:n, done:done};
}
};
}
// Create an Iterator
let iterator = myNumbers[Symbol.iterator]();
let text = ""
while (true) {
const result = iterator.next();
if (result.done) break;
text += result.value +"<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>