In the past, we used a variety of methods to convert things like array objects (such as arguments
and NodeList
) into true values. For example, the following:
- Convert NodeList to an array
Basically, all of these things we can use Array.prototype.slice.call()
in such a way will arguments
and NodeList
so turn into the desired form.
Now, we have a more straightforward way to directly convert the objects of these class arrays into real objects.
Will
NodeList
Transformed into
Array
var divs = Array.from (Document.queryselectorall (' div ')); // array[232] (every DIV on the page)
Will
arguments
Transformed into
Array
function something () { var args = array.from (arguments); // array[' yes ', 1, {}] }something (' yes ', 1, {});
Will
String
Transformed into
Array
// very much like ' JavaScript '. Split (') // ["J", "a", "V", "a", "S", "C", "R", "I", "P", "T"]
These formulations are very simple and clear, no need to use slice
indirect methods such as!
Original address: http://it.lindukj.cn/archives/1405
A new method of converting arrays into JavaScript array.from