In the past, we used a variety of methods to convert things like Array objects (such as arguments and NodeList) into real values. For example:
Convert NodeList to an array
Basically, we can use methods like Array. prototype. slice. call () to convert arguments and NodeList into the desired form.
Now, we have a more direct method to directly convert the objects of these classes into real objects.
Convert NodeList to Array
Var divs = Array. from (document. querySelectorAll ('div '));
// Array [1, 232] (every DIV on the page)
Convert arguments to Array
Function something (){
Var args = Array. from (arguments );
// Array ['Yes', 1, {}]
}
Something ('yes', 1 ,{});
Convert String to Array
Array. from ('javascript '); // similar to 'javascript'. split ('')
// ["J", "a", "v", "a", "S", "c", "r", "I", "p ", "t"]
These statements are simple and clear, and you do not need to use slice or other indirect methods!