What's a better practice, this:
myArray.forEach(function(item)) {
doSomething(item);
function doSomething(an_item) {
console.log(an_item);
}
}
or this:
myArray.forEach(function(item)) {
doSomething(item);
}
function doSomething(an_item) {
console.log(an_item);
}
Does the first example create multiple instances of the function, or does it create it just the first time through the loop?
Thanks for any insight!
myArray.forEach(function(item)) {
doSomething(item);
}
function doSomething(an_item) {
console.log(an_item);
}
this function is best because it will created only one time ;
and
myArray.forEach(function(item)) {
doSomething(item);
function doSomething(an_item) {
console.log(an_item);
}
}
is a bad due to every time function will create during loop process
The second. Use the second form (because the first form will slow down your user's experience, which may well be a phone or low powered device), or the even shorter form
myArray.forEach(doSomething);
function doSomething(element, index, array) {
console.log("myArray[" + index + "] = " + element);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With