Why can't the for of method traverse the modified array?

according to the definition of each method, the following iterative method should be able to run a function on each item in the array, but:

//
//for
let b=[1,2,3]
for(let i=0,len=b.length;i<len;iPP){
    b[i]*=2
    }
console.log(b) //[2,4,6]

//forEach
...
b.forEach(function(i,j){
    b[j]*=2
    })
...//[2,4,6]

//for in
...
let c=Object.keys(b)
for(let i in c){
    b[i]*=2
    }
...//[2,4,6]

//for of
...
for(let i of b){
    i*=2
    }
...//[1,2,3]

you can see that for of will not modify the original array and return it. Referring to many documents, it is found that only traversal adding events are mentioned. Currently, you can only use additional methods on for of to modify the array traversal. Finally, I hope all respondents can give a hint and analysis of the implementation principle of . Thank you

.
Apr.02,2021

the landlord didn't reply to my comments in my doubt article, so I came here to give a strong answer. Haha
the following is personal inference. Please point out

if there is any mistake.

first, take a look at MDN's iterable protocol

.

the following is MDN's iterable protocol:
"the iterable protocol allows JavaScript objects to define or customize their iterative behavior, such as what values can be looped in a for..of structure. Some built-in types are built-in iterable objects and have default iterative behavior, such as Array or Map, while others are not (such as Object). "
here we notice that the iterative behavior of Array and Map is different from that of Object

.

second, look at the nature of the object being iterated

another piece of material:
"when an object needs to be iterated (such as starting to be used in a for..of loop), its @ @ iterator method is called with no arguments, and then returns an iterator to get the value in the iteration."
focuses on "returns an iterator to get the value in the iteration."
here we can see that the essence of for of is to return an iterator

.

third, how does the iterator get the value?

The

iterator returns through the next () method , while does not point to the address , so the array element cannot be changed in the for of iteration

reference material: https://developer.mozilla.org.


map (), filter () can modify arrays. For more information, please see the loop method in https://codeshelper.com/a/11.


.

from top to bottom, 4 methods

  1. are you sure you can run? Even if you can run, return will not cycle. You guessed the result of console,
  2. .
  3. (vQuery I) b [I] * = 2
  4. nothing wrong
  5. Ref. 2.

because you are operating only basic types
var arr= [{}]
for (let i of arr) {
i.aa=1
}


my judgment is that the programming foundation of the subject is still relatively weak, and in this case, you should read books and tutorials. If the foundation is improved, you will naturally understand. Now if you only rely on words to tell you, you may not understand.
in addition, the result of the first example, console.log, should also be [1rect 2pd3]. You may be mistaken. (before the problem is modified)

to answer your question about for-of, for (const an of b) is essentially, for (let i = 0, len = b. Duration, a; I < len; PPi) {a = b [I];.}
Note that the length of b is cached beforehand.

In

JS, there is no such thing as pointer or reference assignment. A pointer [I] does not mean a pointer [I] or a reference [I]. For a base type, it is always a copy of a value, and for an object / array / function, it is always a pass reference.
above a = b [I] is just a copy of the value (because in your case the numbers), an and b [I] are independent of each other.


because for of, forEach makes every item in the item= array, if it is a basic type, then it is a value, and when you re-assign a value to the item, the value of item has nothing to do with each item in the array. But if every item in the array is object, every item in the item= array, item is the address that points to the object, so if you don't reassign the value, you use item.a=1, which is tantamount to modifying the property value of each object in the array

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b380fb-2c0dd.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b380fb-2c0dd.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?