(01-31-2017, 12:26 PM)Vi-Sion Wrote: Your post was very helpfull thank you , one more thing what about arrays ?
array isn't a primitive type but you can do this var nbrs=[1,2,3,4];
There's nothing special about this way of declaring arrays in JavaScript, even though in other languages it might mean something different in Javascript it's all the same. As a consequence, you can expect to be able to use all the Array() Objects methods/prototypes with your array no matter how you create it.
Again, as per Javascript MDN reference (My personal favorite and imo the best js documentation out there)
The documentation describes three ways to create a Javascript Array Object:
Code:
var arr = [element0, element1, ..., elementN];
var arr = new Array(element0, element1[, ...[, elementN]]);
var arr = new Array(arrayLength);
These are all essentially equal and really just depend on your own preferences/use case for declaring a Javascript Array Object.
I suppose the creators of JavaScript did not see a need for a static array implementation, one in which you would not be able to dynamically add or remove elements/change the size of the array since it is stored on the Stack. In other languages, and where I suspect your confusion stems from, a static implementation may be preferred since it's simpler, more efficient, and faster than allocating memory on the heap and dynamically creating your arrray that way. This static implementation would use the syntax for creating an array how you describe. For example, in C creating a static array would look like this:
Code:
int main()
{
static int arr[10]; // static array of size 10 OR
int arr = { 10, 4, 3, 2, 1} // fixed-length array stored on Stack of size 5
}
On the other hand, a dynamic array would look something like this:
Code:
int main()
{
int arr = malloc(10); //array on heap of size 10
}
You could then resize that array through the
method
in Java equal implementations would be:
Code:
int[] arr = {10, 4, 3, 2} //fixed-length array on stack of length 4
ArrayList <Integer> arr = new ArrayList<Integer>(); //in Java 'ArrayList' is the dynamic version of arrays, in this case you can think of 'new' as allocating memory on the Heap
Since JavaScript was created to be largely a web-language, however, the need for this was simply not taken into account.
If you look online, and search "Javascript Arrays" you will probably see a bunch of Blog articles arguing over the different ways to instantiate/create a Javascript array. Some will say "
Code:
var arr = new Array(4);
is superior because your specifying a fixed length and that makes your code faster" or "NEVER create an array like
Code:
var arr = ['el1', 'el2', 'el3', 'el4']
it's BAD" or "ALWAYS USE
Code:
var arr = ['lol', 'the internet never lies', 'bs el']
it's the BEST way!". I like to chock these articles up to psuedo-computer science. It all comes down to the same thing, and unless there is something especially different about a certain javascript engine you are using, then it really doesn't matter and it's nothing to worry about. All these articles serve to do is confuse the humble JavaScript beginner and create pre-existing notions of false concepts.
Create your arrays however you feel is the best way for you. No matter how you do it, it's all going to be the same thing, you'll be able to use the same prototypes, the same methods, and modify them in the same ways.
P.S It's certainly worth reading through the MDN Documentation on the JavaScript Array() Object. The "Description" section may be especially of use when using JavaScript Array properties such as 'length' or clear up some confusion regarding how to access javascript elements (IE: arr[0] and why you can't do arr.0).
https://developer.mozilla.org/en-US/docs...ects/Array