Javascript Array map "appears" to execute callback on missing elements -


map doesn't executed on following array.

array(100).map(function(e,i){return i+1;});

console.log(array(100).map(function(e, i) {    return + 1;  }));

i assume because elements of array 'missing': https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/map

however, map gets executed on elements in following:

array.apply(null,array(100)).map(function(e,i){return i+1;});

console.log(array.apply(null, array(100)).map(function(e, i) {    return + 1;  }));

how in second example, elements of array change 'missing' 'undefined'? (at least assume happening.)

in call

array.apply(null,array(100)).map(function(e,i){return i+1;}); 

the array function called 100 arguments (in fact, length of array(100) 100). when accessing arguments, of them undefined.

if call arbitrary function func(a, b) this:

func.apply(null, array(2)) 

the parameters a , b undefined , length of arguments 2.

map() iterates on elements in array there no elements! however, array has length 100. weird way arrays behave in js. if use array argument list function (via .apply()), arguments accessed , become undefined. original array not change, accessing index in empty array yields undefined.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -