Difference between Iterator and Iterable [duplicate] - javascript

This question already has answers here:
What is the difference between iterator and iterable and how to use them?
(15 answers)
In JavaScript ES6, what is the difference between an iterable and iterator?
(3 answers)
Closed 6 years ago.
What is the difference between Iterator and Iterable? Is one sub-type of the other or not? What are the actual differences in some real applications?
I am trying to read some tutorials and specifications, but they are all very complicated.
(I am using ES6 and Babel, if that helps.)

From Exploring ES6 by Dr. Axel Rauschmayer:
An iterable is a data structure that wants to make its elements accessible to the public. It does so by implementing a method whose key is Symbol.iterator. That method is a factory for iterators.
An iterator is a pointer for traversing the elements of a data structure (think cursors in databases).

Related

For JavaScript, how do properties distinguish from methods? [duplicate]

This question already has answers here:
Example of Properties vs. Methods in JS
(3 answers)
Closed 3 years ago.
In chapter 4 of Eloquent JavaScript, it reads: "[Data types, namely, strings] have built in properties. Every string value has a number of methods. Some very useful ones are slice and index which resemble the array methods of the same name.
For me, the excerpt that I've cited from Eloquent JavaScript appears to use the terms "property" and "method" interchangeably.
From MDN Web Docs, I understand that "A JavaScript property is a characteristic of an object, often describing attributes associated with a data structure.
Additionally, MDN's glossary defines a method as a function which is a property of an object.
Can anyone assist in distinguishing the terms "property" and "method" (as used in JavaScript) from one another?
Short answer
Property is something "about" the string e.g. its length.
Method is something that can be done to the string e.g. slice

JavaScript Objects built on arrays [duplicate]

This question already has answers here:
Why can I add named properties to an array as if it were an object?
(8 answers)
Difference between array and object in javascript? or Array Vs Object
(4 answers)
Associative array versus object in JavaScript
(7 answers)
Closed 4 years ago.
From my readings, in JavaScript:
Objects = Hash Tables, which are build on Arrays. However, it is commonly said that Arrays are Objects in JS. How are these two concepts reconciled?
Objects are not built on arrays. Objects have their own optimizations.
In general:
Objects are for "structs", structures of predictable "shape" and keys known in advance (even though they can be used with dynamic keys, you should use Maps for that. See below).
Arrays are for lists (and queues, and stacks), structures where the keys are numbers, or where the order of elements matters. Arrays are "special" objects, not the other way around. (You can put string-based properties on an array, just like any object. Please don't do that though).
Maps are for hash tables/dictionaries, structures where the keys are dynamic and not known in advance.

How to make weak set or weak map iterable in ES6? [duplicate]

This question already has answers here:
Why will ES6 WeakMap's not be enumerable?
(2 answers)
Closed 6 years ago.
How to make weak set or weak map "iterable" in ES6 so that i can use for in loop :
for(item in weakMap){console.log(item); }
No the contents of a WeakMap are not accessible by design, and there is no iterability.
A key property of Weak Maps is the inability to enumerate their keys.
This is necessary to prevent attackers observing the internal behavior
of other systems in the environment which share weakly-mapped objects.
Should the number or names of items in the collection be discoverable
from the API, even if the values aren't, WeakMap instances might
create a side channel where one was previously not available.
Source

When would you use an ES6 Map over an Object? [duplicate]

This question already has answers here:
Map vs Object in JavaScript
(15 answers)
Closed 7 years ago.
I've been looking at the new ES6 docs on MDN and I can't find a real world use for the Map object where a normal object wouldn't work. Does anyone have any use cases for Maps and explain why an object wouldn't work in that scenario?
MDN lists a number of important differences:
An Object has a prototype, so there are default keys in the map. [editor: the good old hasOwnProperty issue]
The keys of an Object are Strings, where they can be any value for a Map.
You can get the size of a Map easily while you have to manually keep track of size for an Object.
A Map iterates its elements in insertion order, whereas iteration order is not specified for Objects.
So a Map is finally an insert-ordered key-value store for Javascript, which additionally allows mapping any value to any value, instead of restricting keys to be strings. This can greatly simplify some code where ordering is important, or where objects or other complex data types need to be associated with other data.

How do I reverse() an associative array (object)? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Loop through associative array in reverse
I have a key-value pair javascript object.
How do I reverse() it? or loop it starting from the bottom.
jQuery available.
There is no hard specification or guarantee of determining the order of properties in a Javascript object. Most browsers go from what was added first to last, but as Elements order in a "for (… in …)" loop
describes, Chrome and Opera do not.
If order is important to you, then you must use an array, or have an array that lists the order in which the properties should be accessed when looping (from either direction).

Categories

Resources