I know that document.querySelectorAll('p') gets a NodeList, but it has been deconstructed using... and then put into []. What it gets is an array, right?
//Updated the title. . It seems that no one understands what I mean. .
巴扎黑2017-06-12 09:31:50
NodeList objects are collections of nodes such as those returned by properties such as Node.childNodes and the document.querySelectorAll() method
What is returned is a NodeList object, not an array. It just has some methods and properties that an array has, so it is similar to an array
Update:
After you revised the question, I actually feel that your idea is right. Maybe Teacher Ruan’s writing here is inaccurate. If the code is like this, there will be no problem:
const set = new Set(document.querySelectorAll('p'))
You can leave him a message below and he will change it after seeing it.
阿神2017-06-12 09:31:50
...
is the spread
operator, used to spread arrays or array-like objects.
Expand each item into a new array, so the return value here is the array object of all p.
document.querySelectorAll
returns an array-like object. Array-like objects are not instances of array types, so you cannot use array methods on them, such as splice
, map
, etc. These methods can be used after converting a real array.
迷茫2017-06-12 09:31:50
Yes, the result of document.querySelectorAll() is in the form of an array. The id returned is also an array. You can print it and see it yourself
世界只因有你2017-06-12 09:31:50
querySelectAll returns a NodeList, not an array, but an array-like array.
淡淡烟草味2017-06-12 09:31:50
He is generating a new array from the node and returning it. It is an array. I guess he is talking about an array-like structure like nodelist.
阿神2017-06-12 09:31:50
Not an array.
This thing is called nodelist
Why is NodeList not an array?
The NodeList object is very similar to an array in some aspects, and it seems that the methods inherited from Array.prototype can be used directly. However, NodeList does not have these array-like methods.
Look at mdn, Nodelist
==========
Well, yes, it’s an array. So what does the questioner want to ask?