• for.. Can “of” traverse objects?
  • for… In and for… What’s the difference between “of”?
  • What are enumerable and iterable?

If the above questions are clear, then do not need to look down.

for… in

for… In traverses the enumerable properties of an object in any order;

Note:

  1. Except for the Symbol attribute.
  2. It can traverse both the enumerable properties of the object itself and the enumerable properties of the object prototype. You can use getOwnPropertyNames() or hasOwnProperty() to determine if a property is a property of the object itself.
  3. for… In is built to iterate over an object’s properties and is not recommended for use with arrays. Arrays are available for forEach and for… Of.

Can be enumerated

Enumerable properties are those whose internal enumerable flag is set to true; Enumerable is a property that is initialized directly by assignment and property. Enumerable defaults to true; For attributes defined via object.defineProperty, the identity defaults to false.

for… of

for… Of creates an iteration loop on the iterable, calls the custom iteration hook, and executes a statement for the value of each different property

Note:

  1. Object iteration (including Array, Map, Set, String, TypedArray, the arguments object, etc.)
  2. Normally created objects cannot be created by for… Of iterative, unless the object is constructed as an iterable;

Can the iteration

An iterable is an object that has an @@iterator method built in or implemented by itself; That is, the object must have a property whose key is @@iterator, accessible through the constant symbol. iterator; Iterator [Symbol. Iterator] is a function with no arguments. The key is that the function must return an iterator object.

Iterator object

An iterator object is simply an object that has a next() method; Also, the next() method must be a no-argument function and return an object with the following two properties:

  • Done: False if the iterator can produce the next value of the sequence. True if the iterator has iterated through the sequence. In this case, value is optional, if it still exists, which is the default value at the end of the iteration.
  • Value: Any JavaScript value returned by the iterator. Done can be omitted when true

Note: The next() method must return an object with two properties: done and value, and raises a TypeError exception if it returns a non-object value (false or undefined)

let o={};// This is not iterable
o[Symbol.iterator]=function(){
    return {
        next:function(){
            if(this.index<3) {return {
                value:this.index++,
                done:false}}else{
            return {
                done:true}}},index:0}}// Now the object o is traversable and can be used for... Of and expand syntax operations...
for(let key in o){
console.log(o)
}/ / logs: 0
Copy the code

Conclusion:

  • If an ordinary Object wants to be iterable, it must define a [symbol.iterator] property whose value is a no-argument function that returns an Object with a next() method. The next() method is also a no-argument function and returns an object with attribute values value and done.
  • for… In and for… The difference of of is in the way it iterates. The former iterates over the enumerable properties of an object in any order. The latter iterates over the iterable defining the data to be iterated over.