I’m trying to understand what’s inside an iterator object.
Q: How can I see every property/method of an object, in this case an iteration object?
How can I see exactly of what an object is made of?
Why is the .next() method not listed by the Object.getOwnPropertyNames() method?
I thought that every method in an object needs to be attached to a key inside that object?
The only keys that show up are the ones(color, taste) I added myself.
Below the html code and the rendered page:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The matchAll() Method</h2>
<p>ES2020 intoduced the string method matchAll().</p>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular."
const iterator = text.matchAll(/Cats/gi);
// If I can add a properties, then it must be an object.
iterator.color = "green";
iterator.taste = "salty";
let text2 = "<hr>";
text2 += "Iterator object entries => ";
for (let [key, value] of Object.entries(iterator)) {
text2 += key + ": " + value + ", ";
}
text2 += "<br><br>";
text2 += "Print out the iterator object directly => " + iterator + "<br><br>";
text2 += "Iterator object keys => " + Object.keys(iterator) + "<br><br>";
text2 += "Iterator object values => " + Object.values(iterator) + "<br><br>";
text2 += "Stringified JSON => " + JSON.stringify(iterator) + "<br><br>";
text2 += "Object.getOwnPropertyNames(iterator) => " + Object.getOwnPropertyNames(iterator) + "<br><br>";
// Intentionally calling .next() Method to "consume" one iteration.
iterator.next();
text2 += "Array.from(iterator) => " + Array.from(iterator) + "<br>";
document.getElementById("demo").innerHTML = text2;
</script>
</body>
</html>
The rendered page looks something like this:
JavaScript Strings
The matchAll() Method
ES2020 intoduced the string method matchAll().
Iterator object entries => color: green, taste: salty,
Print out the iterator object directly => [object RegExp String Iterator]
Iterator object keys => color,taste
Iterator object values => green,salty
Stringified JSON => {"color":"green","taste":"salty"}
Object.getOwnPropertyNames(iterator) => color,taste
Array.from(iterator) => Cats,Cats