It would seem that this method was introduced as a way to provide something that is missing when using the Array constructor directly with one argument. That is calling the main Array constructor method with the new keyword as a way to create a new instance of an array, rather than using the bracket syntax to do so. When using the Array constructor with new there is using just one optional argument that is a number rather than some other value when doing so this sets a starting length of the array, but not the first value of the array.
I can not say that I use the Array of method often, as I prefer to use some of the older tired yet true ways of doing the same thing that is just a little more involved. But never the less this post will be on the JS array of meto9d and other ways of creating a new array with a set number of starting values.
Before the Array of method there where two general ways of going about creating a new Array. One way is to use the array bracket syntax and the other is to use the Array constructor function. The Array constructor function works okay, but there is just one little issue with the first argument. In this section I will be going over what that little issue is and how the array of method can be used as one way to address that issue when it comes to additional ways to create a new array.
When using the Array constructor with the new keyword as a way to create a new array there is a problem when passing one argument that is a number. many developers might thing that by doing so they will end up getting a new array with the first element being the number value that is given as the first, and only argument. However this is n ot the case, instead what happens is one ends up with a new empty array with a length property set to the number that is given as that one and only argument.
there are ways of using the Array constructor to get the desired result, more on that later. However there is still the question of creating a new array by way of one or more arguments that are numbers and having those numbers always be starting elements rather than a length value if it is just one argument. This is where the array of method might be of use.
So if the array of static method is called and a single argument is given that is a number, then the retured result will be a new array with that number value being the value of the first argument.
The tired yet true Array constructor can still be used to create a new array and have just one element that is a number. The only thing to be aware of is what happens wheh it is given just one argument that is a number. If you just know about that and find ways to ajust for it then there is no need to bother with the array of method really.
There are also other ways of creating an array that are tired yet true that do not have this problem going on, but for now lets take a another look at the Array constructor.
So if the first argument is number that will be used to set the length of the new array, so one way to get around this is to just make it so the first argument is not a number.
The effect where the first argument is used to set the starting length of the new array is only going to happen if it is a number, but not a string of that number. So if I just pass a string as the first argument rather than a number then that string will be used to set the value of the first argument.
There is using some kind of method to help address the problem of the number value being in a nested array when using an array to set the value of the first argument. One such method that coes to mind is the array mao method, but theer are a number of other options that would work.
The issue if a combination of using a number as the first argument, but also only passing one argument. So when passing two or more arguments for starting values there is no problem. If for some reason you only want one starting value in the array that is a number you could still use this to do that by just popping out the extra element.
yet another options is to just give no arguments at all to start with and just push in the first element after creating an empty array.
I still thing that the best way to go about creating new arrays is to just use the bracket syntax. If I want an array with a single starting element that is a number then I can just place that number in between an object and closing set of square brackets and move on to far more important things then this.
So another Static Array method of interest here now it the Array from method. Just like the Array of method it can be used to create a new Array, but it does so by way of creating an array from a source object. When it comes to source Objects many such Objects might be Array like Objects. That is that they are just Plain old objects, or Objects that where crated with a constructor other then that of the Array constructor. As such they do not have Array prototype methods because they are not an instance of the Array constructor. So the Array from method is one way to create a new array by way of passing one of these objects as the first argument.
Ask yourself this when creating a new array \”What is the goal here?\”. Often the goal with all of this is to just simple create a new array, rather than mutating an array that all ready exists. In that situation using a method like Array of, or the bracket syntax will do just that. However there are many methods in the Array prototype that will return a new Array rather than mutating an array in place. One such method is the Array map method.